# random_func.hpp

Go to the documentation of this file.

    00001 /* $Id: random_func.hpp 16060 2009-04-14 21:13:07Z rubidium $ */
    00002
    00005 #ifndef RANDOM_FUNC_HPP
    00006 #define RANDOM_FUNC_HPP
    00007
    00008 #if defined(__APPLE__)
    00009   /* Apple already has Random declared */
    00010   #define Random OTTD_Random
    00011 #endif /* __APPLE__ */
    00012
    00013 /**************
    00014  * Warning: DO NOT enable this unless you understand what it does
    00015  *
    00016  * If enabled, in a network game all randoms will be dumped to the
    00017  *  stdout if the first client joins (or if you are a client). This
    00018  *  is to help finding desync problems.
    00019  *
    00020  * Warning: DO NOT enable this unless you understand what it does
    00021  **************/
    00022
    00023 //#define RANDOM_DEBUG
    00024
    00025
    00029 struct Randomizer {
    00031   uint32 state[2];
    00032
    00037   uint32 Next();
    00038
    00044   uint32 Next(uint16 max);
    00045
    00050   void SetSeed(uint32 seed);
    00051 };
    00052 extern Randomizer _random;
    00053 extern Randomizer _interactive_random;
    00054
    00056 struct SavedRandomSeeds {
    00057   Randomizer random;
    00058   Randomizer interactive_random;
    00059 };
    00060
    00064 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
    00065 {
    00066   storage->random = _random;
    00067   storage->interactive_random = _interactive_random;
    00068 }
    00069
    00073 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
    00074 {
    00075   _random = storage.random;
    00076   _interactive_random = storage.interactive_random;
    00077 }
    00078
    00079 void SetRandomSeed(uint32 seed);
    00080 #ifdef RANDOM_DEBUG
    00081   #define Random() DoRandom(__LINE__, __FILE__)
    00082   uint32 DoRandom(int line, const char *file);
    00083   #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
    00084   uint DoRandomRange(uint max, int line, const char *file);
    00085 #else
    00086   static FORCEINLINE uint32 Random()
    00087   {
    00088     return _random.Next();
    00089   }
    00090
    00091   static FORCEINLINE uint32 RandomRange(uint16 max)
    00092   {
    00093     return _random.Next(max);
    00094   }
    00095 #endif
    00096
    00097 static FORCEINLINE uint32 InteractiveRandom()
    00098 {
    00099   return _interactive_random.Next();
    00100 }
    00101
    00102 static FORCEINLINE uint32 InteractiveRandomRange(uint16 max)
    00103 {
    00104   return _interactive_random.Next(max);
    00105 }
    00106
    00122 static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r)
    00123 {
    00124   assert(b != 0);
    00125   return (((uint16)r * b + b / 2) >> 16) < a;
    00126 }
    00127
    00140 static FORCEINLINE bool Chance16(const uint a, const uint b)
    00141 {
    00142   return Chance16I(a, b, Random());
    00143 }
    00144
    00160 static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r)
    00161 {
    00162   r = Random();
    00163   return Chance16I(a, b, r);
    00164 }
    00165
    00166 #endif /* RANDOM_FUNC_HPP */