skidd13@8431: /* $Id$ */ skidd13@8431: rubidium@10429: /** @file random_func.hpp Pseudo random number generator. */ skidd13@8431: skidd13@8431: #ifndef RANDOM_FUNC_HPP skidd13@8431: #define RANDOM_FUNC_HPP skidd13@8431: skidd13@8622: #if defined(__APPLE__) skidd13@8622: /* Apple already has Random declared */ skidd13@8622: #define Random OTTD_Random skidd13@8622: #endif /* __APPLE__ */ skidd13@8622: skidd13@8431: /************** skidd13@8431: * Warning: DO NOT enable this unless you understand what it does skidd13@8431: * skidd13@8431: * If enabled, in a network game all randoms will be dumped to the skidd13@8431: * stdout if the first client joins (or if you are a client). This skidd13@8431: * is to help finding desync problems. skidd13@8431: * skidd13@8431: * Warning: DO NOT enable this unless you understand what it does skidd13@8431: **************/ skidd13@8431: skidd13@8431: //#define RANDOM_DEBUG skidd13@8431: skidd13@8431: rubidium@8930: /** rubidium@8930: * Structure to encapsulate the pseudo random number generators. rubidium@8930: */ rubidium@8930: struct Randomizer { rubidium@8930: /** The state of the randomizer */ rubidium@8930: uint32 state[2]; rubidium@8930: rubidium@8930: /** rubidium@8930: * Generate the next pseudo random number rubidium@8930: * @return the random number rubidium@8930: */ rubidium@8930: uint32 Next(); rubidium@8930: rubidium@8930: /** rubidium@8930: * Generate the next pseudo random number scaled to max rubidium@8930: * @param max the maximum value of the returned random number rubidium@8930: * @return the random number rubidium@8930: */ rubidium@8930: uint32 Next(uint16 max); rubidium@8930: rubidium@8930: /** rubidium@8930: * (Re)set the state of the random number generator. rubidium@8930: * @param seed the new state rubidium@8930: */ rubidium@8930: void SetSeed(uint32 seed); rubidium@8930: }; rubidium@8930: extern Randomizer _random; ///< Random used in the game state calculations rubidium@8930: extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state rubidium@8930: skidd13@8431: void SetRandomSeed(uint32 seed); skidd13@8431: #ifdef RANDOM_DEBUG skidd13@8431: #define Random() DoRandom(__LINE__, __FILE__) skidd13@8431: uint32 DoRandom(int line, const char *file); skidd13@8431: #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) skidd13@8431: uint DoRandomRange(uint max, int line, const char *file); skidd13@8431: #else skidd13@11050: static FORCEINLINE uint32 Random() skidd13@11050: { skidd13@11050: return _random.Next(); skidd13@11050: } skidd13@11050: skidd13@11050: static FORCEINLINE uint32 RandomRange(uint16 max) skidd13@11050: { skidd13@11050: return _random.Next(max); skidd13@11050: } skidd13@8431: #endif skidd13@8431: skidd13@11050: static FORCEINLINE uint32 InteractiveRandom() skidd13@11050: { skidd13@11050: return _interactive_random.Next(); skidd13@11050: } skidd13@11050: skidd13@11050: static FORCEINLINE uint32 InteractiveRandomRange(uint16 max) skidd13@11050: { skidd13@11050: return _interactive_random.Next(max); skidd13@11050: } skidd13@8431: skidd13@8463: /** skidd13@8463: * Checks if a given randomize-number is below a given probability. skidd13@8463: * skidd13@8463: * This function is used to check if the given probability by the fraction of (a/b) skidd13@8463: * is greater than low 16 bits of the given randomize-number v. skidd13@8463: * skidd13@8463: * Do not use this function twice on the same random 16 bits as it will yield skidd13@8463: * the same result. One can use a random number for two calls to Chance16I, skidd13@8463: * where one call sends the low 16 bits and the other the high 16 bits. skidd13@8463: * skidd13@8463: * @param a The numerator of the fraction skidd13@8463: * @param b The denominator of the fraction, must of course not be null skidd13@8463: * @param r The given randomize-number skidd13@8463: * @return True if v is less or equals (a/b) skidd13@8463: */ skidd13@11049: static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r) skidd13@8463: { skidd13@8463: assert(b != 0); smatz@9071: return (uint16)r < (uint16)(((a << 16) + b / 2) / b); skidd13@8463: } skidd13@8463: skidd13@8463: /** skidd13@8463: * Flips a coin with a given probability. skidd13@8463: * skidd13@8463: * This macro can be used to get true or false randomized according to a skidd13@8463: * given probability. The parameter a and b create a percent value with skidd13@8463: * (a/b). The macro returns true in (a/b) percent. skidd13@8463: * skidd13@8463: * @see Chance16I() skidd13@8463: * @param a The numerator of the fraction skidd13@8463: * @param b The denominator of the fraction skidd13@8463: * @return True in (a/b) percent skidd13@8463: */ skidd13@11049: static FORCEINLINE bool Chance16(const uint a, const uint b) skidd13@8463: { skidd13@8463: return Chance16I(a, b, Random()); skidd13@8463: } skidd13@8463: skidd13@8463: /** skidd13@8463: * Flips a coin with a given probability and saves the randomize-number in a variable. skidd13@8463: * skidd13@8463: * This function uses the same parameters as Chance16. The third parameter skidd13@8463: * must be a variable the randomize-number from Random() is saved in. skidd13@8463: * skidd13@8463: * The low 16 bits of r will already be used and can therefor not be passed to skidd13@8463: * Chance16I. One can only send the high 16 bits to Chance16I. skidd13@8463: * skidd13@8463: * @see Chance16I() skidd13@8463: * @param a The numerator of the fraction skidd13@8463: * @param b The denominator of the fraction skidd13@8463: * @param r The variable to save the randomize-number from Random() skidd13@8463: * @return True in (a/b) percent skidd13@8463: */ skidd13@11049: static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r) skidd13@8463: { skidd13@8463: r = Random(); skidd13@8463: return Chance16I(a, b, r); skidd13@8463: } skidd13@8463: skidd13@8431: #endif /* RANDOM_FUNC_HPP */