summaryrefslogtreecommitdiff
path: root/src/core/random_func.hpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-01-29 00:27:25 +0000
committerrubidium <rubidium@openttd.org>2008-01-29 00:27:25 +0000
commit3b2145aafe29d6abd8aeef3dccdb5cf587f111d2 (patch)
treea27a03588a37fa3492201013f224c9fdf751ecc3 /src/core/random_func.hpp
parentfde33b554790379af624e3c4c548535ca4097546 (diff)
downloadopenttd-3b2145aafe29d6abd8aeef3dccdb5cf587f111d2.tar.xz
(svn r12004) -Codechange: refactor the random functions to reduce code duplication.
Diffstat (limited to 'src/core/random_func.hpp')
-rw-r--r--src/core/random_func.hpp39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/core/random_func.hpp b/src/core/random_func.hpp
index f3d35d987..d941f283c 100644
--- a/src/core/random_func.hpp
+++ b/src/core/random_func.hpp
@@ -27,6 +27,35 @@
// Doesn't work with network yet.
// #define MERSENNE_TWISTER
+/**
+ * Structure to encapsulate the pseudo random number generators.
+ */
+struct Randomizer {
+ /** The state of the randomizer */
+ uint32 state[2];
+
+ /**
+ * Generate the next pseudo random number
+ * @return the random number
+ */
+ uint32 Next();
+
+ /**
+ * Generate the next pseudo random number scaled to max
+ * @param max the maximum value of the returned random number
+ * @return the random number
+ */
+ uint32 Next(uint16 max);
+
+ /**
+ * (Re)set the state of the random number generator.
+ * @param seed the new state
+ */
+ void SetSeed(uint32 seed);
+};
+extern Randomizer _random; ///< Random used in the game state calculations
+extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state
+
void SetRandomSeed(uint32 seed);
#ifdef RANDOM_DEBUG
#define Random() DoRandom(__LINE__, __FILE__)
@@ -34,12 +63,12 @@ void SetRandomSeed(uint32 seed);
#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
uint DoRandomRange(uint max, int line, const char *file);
#else
- uint32 Random();
- uint RandomRange(uint max);
+ static inline uint32 Random() { return _random.Next(); }
+ static inline uint32 RandomRange(uint16 max) { return _random.Next(max); }
#endif
-uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link
-uint InteractiveRandomRange(uint max);
+static inline uint32 InteractiveRandom() { return _interactive_random.Next(); }
+static inline uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); }
/**
* Checks if a given randomize-number is below a given probability.
@@ -100,6 +129,4 @@ static inline bool Chance16R(const uint a, const uint b, uint32 &r)
return Chance16I(a, b, r);
}
-extern uint32 _random_seeds[2][2];
-
#endif /* RANDOM_FUNC_HPP */