skidd13@7935: /* $Id$ */ skidd13@7935: skidd13@7935: /** @file random_func.h */ skidd13@7935: skidd13@7935: #ifndef RANDOM_FUNC_HPP skidd13@7935: #define RANDOM_FUNC_HPP skidd13@7935: skidd13@7935: /************** skidd13@7935: * Warning: DO NOT enable this unless you understand what it does skidd13@7935: * skidd13@7935: * If enabled, in a network game all randoms will be dumped to the skidd13@7935: * stdout if the first client joins (or if you are a client). This skidd13@7935: * is to help finding desync problems. skidd13@7935: * skidd13@7935: * Warning: DO NOT enable this unless you understand what it does skidd13@7935: **************/ skidd13@7935: skidd13@7935: //#define RANDOM_DEBUG skidd13@7935: skidd13@7935: skidd13@7935: // Enable this to produce higher quality random numbers. skidd13@7935: // Doesn't work with network yet. skidd13@7935: // #define MERSENNE_TWISTER skidd13@7935: skidd13@7935: void SetRandomSeed(uint32 seed); skidd13@7935: #ifdef RANDOM_DEBUG skidd13@7935: #define Random() DoRandom(__LINE__, __FILE__) skidd13@7935: uint32 DoRandom(int line, const char *file); skidd13@7935: #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) skidd13@7935: uint DoRandomRange(uint max, int line, const char *file); skidd13@7935: #else skidd13@7935: uint32 Random(); skidd13@7935: uint RandomRange(uint max); skidd13@7935: #endif skidd13@7935: skidd13@7935: uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link skidd13@7935: uint InteractiveRandomRange(uint max); skidd13@7935: skidd13@7967: /** skidd13@7967: * Checks if a given randomize-number is below a given probability. skidd13@7967: * skidd13@7967: * This function is used to check if the given probability by the fraction of (a/b) skidd13@7967: * is greater than low 16 bits of the given randomize-number v. skidd13@7967: * skidd13@7967: * Do not use this function twice on the same random 16 bits as it will yield skidd13@7967: * the same result. One can use a random number for two calls to Chance16I, skidd13@7967: * where one call sends the low 16 bits and the other the high 16 bits. skidd13@7967: * skidd13@7967: * @param a The numerator of the fraction skidd13@7967: * @param b The denominator of the fraction, must of course not be null skidd13@7967: * @param r The given randomize-number skidd13@7967: * @return True if v is less or equals (a/b) skidd13@7967: */ skidd13@7967: static inline bool Chance16I(const uint a, const uint b, const uint32 r) skidd13@7967: { skidd13@7967: assert(b != 0); skidd13@7967: return (uint16)r < (uint16)((a << 16) / b); skidd13@7967: } skidd13@7967: skidd13@7967: /** skidd13@7967: * Flips a coin with a given probability. skidd13@7967: * skidd13@7967: * This macro can be used to get true or false randomized according to a skidd13@7967: * given probability. The parameter a and b create a percent value with skidd13@7967: * (a/b). The macro returns true in (a/b) percent. skidd13@7967: * skidd13@7967: * @see Chance16I() skidd13@7967: * @param a The numerator of the fraction skidd13@7967: * @param b The denominator of the fraction skidd13@7967: * @return True in (a/b) percent skidd13@7967: */ skidd13@7967: static inline bool Chance16(const uint a, const uint b) skidd13@7967: { skidd13@7967: return Chance16I(a, b, Random()); skidd13@7967: } skidd13@7967: skidd13@7967: /** skidd13@7967: * Flips a coin with a given probability and saves the randomize-number in a variable. skidd13@7967: * skidd13@7967: * This function uses the same parameters as Chance16. The third parameter skidd13@7967: * must be a variable the randomize-number from Random() is saved in. skidd13@7967: * skidd13@7967: * The low 16 bits of r will already be used and can therefor not be passed to skidd13@7967: * Chance16I. One can only send the high 16 bits to Chance16I. skidd13@7967: * skidd13@7967: * @see Chance16I() skidd13@7967: * @param a The numerator of the fraction skidd13@7967: * @param b The denominator of the fraction skidd13@7967: * @param r The variable to save the randomize-number from Random() skidd13@7967: * @return True in (a/b) percent skidd13@7967: */ skidd13@7967: static inline bool Chance16R(const uint a, const uint b, uint32 &r) skidd13@7967: { skidd13@7967: r = Random(); skidd13@7967: return Chance16I(a, b, r); skidd13@7967: } skidd13@7967: skidd13@7935: #endif /* RANDOM_FUNC_HPP */