25 |
25 |
26 // Enable this to produce higher quality random numbers. |
26 // Enable this to produce higher quality random numbers. |
27 // Doesn't work with network yet. |
27 // Doesn't work with network yet. |
28 // #define MERSENNE_TWISTER |
28 // #define MERSENNE_TWISTER |
29 |
29 |
|
30 /** |
|
31 * Structure to encapsulate the pseudo random number generators. |
|
32 */ |
|
33 struct Randomizer { |
|
34 /** The state of the randomizer */ |
|
35 uint32 state[2]; |
|
36 |
|
37 /** |
|
38 * Generate the next pseudo random number |
|
39 * @return the random number |
|
40 */ |
|
41 uint32 Next(); |
|
42 |
|
43 /** |
|
44 * Generate the next pseudo random number scaled to max |
|
45 * @param max the maximum value of the returned random number |
|
46 * @return the random number |
|
47 */ |
|
48 uint32 Next(uint16 max); |
|
49 |
|
50 /** |
|
51 * (Re)set the state of the random number generator. |
|
52 * @param seed the new state |
|
53 */ |
|
54 void SetSeed(uint32 seed); |
|
55 }; |
|
56 extern Randomizer _random; ///< Random used in the game state calculations |
|
57 extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state |
|
58 |
30 void SetRandomSeed(uint32 seed); |
59 void SetRandomSeed(uint32 seed); |
31 #ifdef RANDOM_DEBUG |
60 #ifdef RANDOM_DEBUG |
32 #define Random() DoRandom(__LINE__, __FILE__) |
61 #define Random() DoRandom(__LINE__, __FILE__) |
33 uint32 DoRandom(int line, const char *file); |
62 uint32 DoRandom(int line, const char *file); |
34 #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) |
63 #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) |
35 uint DoRandomRange(uint max, int line, const char *file); |
64 uint DoRandomRange(uint max, int line, const char *file); |
36 #else |
65 #else |
37 uint32 Random(); |
66 static inline uint32 Random() { return _random.Next(); } |
38 uint RandomRange(uint max); |
67 static inline uint32 RandomRange(uint16 max) { return _random.Next(max); } |
39 #endif |
68 #endif |
40 |
69 |
41 uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link |
70 static inline uint32 InteractiveRandom() { return _interactive_random.Next(); } |
42 uint InteractiveRandomRange(uint max); |
71 static inline uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); } |
43 |
72 |
44 /** |
73 /** |
45 * Checks if a given randomize-number is below a given probability. |
74 * Checks if a given randomize-number is below a given probability. |
46 * |
75 * |
47 * This function is used to check if the given probability by the fraction of (a/b) |
76 * This function is used to check if the given probability by the fraction of (a/b) |