1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #ifndef RANDOM_HXX |
19 | #define RANDOM_HXX |
20 | |
21 | #include "bspf.hxx" |
22 | #include "Serializable.hxx" |
23 | |
24 | /** |
25 | This is a quick-and-dirty random number generator. It is based on |
26 | information in Chapter 7 of "Numerical Recipes in C". It's a simple |
27 | linear congruential generator. |
28 | |
29 | @author Bradford W. Mott |
30 | */ |
31 | class Random : public Serializable |
32 | { |
33 | public: |
34 | /** |
35 | Create a new random number generator |
36 | */ |
37 | explicit Random(uInt32 seed) { initSeed(seed); } |
38 | |
39 | /** |
40 | Re-initialize the random number generator with a new seed, |
41 | to generate a different set of random numbers. |
42 | */ |
43 | void initSeed(uInt32 seed) |
44 | { |
45 | myValue = seed; |
46 | } |
47 | |
48 | /** |
49 | Answer the next random number from the random number generator |
50 | |
51 | @return A random number |
52 | */ |
53 | uInt32 next() const |
54 | { |
55 | return (myValue = (myValue * 2416 + 374441) % 1771875); |
56 | } |
57 | |
58 | /** |
59 | Save the current state of this device to the given Serializer. |
60 | |
61 | @param out The Serializer object to use |
62 | @return False on any errors, else true |
63 | */ |
64 | bool save(Serializer& out) const override |
65 | { |
66 | try |
67 | { |
68 | out.putInt(myValue); |
69 | } |
70 | catch(...) |
71 | { |
72 | cerr << "ERROR: Random::save" << endl; |
73 | return false; |
74 | } |
75 | |
76 | return true; |
77 | } |
78 | |
79 | /** |
80 | Load the current state of this device from the given Serializer. |
81 | |
82 | @param in The Serializer object to use |
83 | @return False on any errors, else true |
84 | */ |
85 | bool load(Serializer& in) override |
86 | { |
87 | try |
88 | { |
89 | myValue = in.getInt(); |
90 | } |
91 | catch(...) |
92 | { |
93 | cerr << "ERROR: Random::load" << endl; |
94 | return false; |
95 | } |
96 | |
97 | return true; |
98 | } |
99 | |
100 | private: |
101 | // Indicates the next random number |
102 | // We make this mutable, since it's not immediately obvious that |
103 | // calling next() should change internal state (ie, the *logical* |
104 | // state of the object shouldn't change just by asking for another |
105 | // random number) |
106 | mutable uInt32 myValue; |
107 | |
108 | private: |
109 | // Following constructors and assignment operators not supported |
110 | Random() = delete; |
111 | Random(const Random&) = delete; |
112 | Random(Random&&) = delete; |
113 | Random& operator=(const Random&) = delete; |
114 | Random& operator=(Random&&) = delete; |
115 | }; |
116 | |
117 | #endif |
118 | |