OpenTTD
random_func.hpp
Go to the documentation of this file.
1 /* $Id: random_func.hpp 25893 2013-10-20 14:48:08Z fonsinchen $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef RANDOM_FUNC_HPP
13 #define RANDOM_FUNC_HPP
14 
15 #if defined(__APPLE__)
16  /* Apple already has Random declared */
17  #define Random OTTD_Random
18 #endif /* __APPLE__ */
19 
23 struct Randomizer {
25  uint32 state[2];
26 
27  uint32 Next();
28  uint32 Next(uint32 limit);
29  void SetSeed(uint32 seed);
30 };
31 extern Randomizer _random;
33 
36  Randomizer random;
37  Randomizer interactive_random;
38 };
39 
44 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
45 {
46  storage->random = _random;
47  storage->interactive_random = _interactive_random;
48 }
49 
54 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
55 {
56  _random = storage.random;
57  _interactive_random = storage.interactive_random;
58 }
59 
60 void SetRandomSeed(uint32 seed);
61 #ifdef RANDOM_DEBUG
62  #ifdef __APPLE__
63  #define OTTD_Random() DoRandom(__LINE__, __FILE__)
64  #else
65  #define Random() DoRandom(__LINE__, __FILE__)
66  #endif
67  uint32 DoRandom(int line, const char *file);
68  #define RandomRange(limit) DoRandomRange(limit, __LINE__, __FILE__)
69  uint32 DoRandomRange(uint32 limit, int line, const char *file);
70 #else
71  static inline uint32 Random()
72  {
73  return _random.Next();
74  }
75 
83  static inline uint32 RandomRange(uint32 limit)
84  {
85  return _random.Next(limit);
86  }
87 #endif
88 
89 static inline uint32 InteractiveRandom()
90 {
91  return _interactive_random.Next();
92 }
93 
94 static inline uint32 InteractiveRandomRange(uint32 limit)
95 {
96  return _interactive_random.Next(limit);
97 }
98 
114 static inline bool Chance16I(const uint a, const uint b, const uint32 r)
115 {
116  assert(b != 0);
117  return (((uint16)r * b + b / 2) >> 16) < a;
118 }
119 
130 #ifdef RANDOM_DEBUG
131  #define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
132 #else
133 static inline bool Chance16(const uint a, const uint b)
134 {
135  return Chance16I(a, b, Random());
136 }
137 #endif /* RANDOM_DEBUG */
138 
154 #ifdef RANDOM_DEBUG
155  #define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
156 #else
157 static inline bool Chance16R(const uint a, const uint b, uint32 &r)
158 {
159  r = Random();
160  return Chance16I(a, b, r);
161 }
162 #endif /* RANDOM_DEBUG */
163 
164 #endif /* RANDOM_FUNC_HPP */
static bool Chance16I(const uint a, const uint b, const uint32 r)
Checks if a given randomize-number is below a given probability.
Stores the state of all random number generators.
Definition: random_func.hpp:35
static void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Definition: random_func.hpp:54
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:83
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:57
Randomizer _interactive_random
Random used every else where is does not (directly) influence the game state.
Definition: random_func.cpp:27
static bool Chance16R(const uint a, const uint b, uint32 &r)
Flips a coin with a given probability and saves the randomize-number in a variable.
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:27
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:67
uint32 Next()
Generate the next pseudo random number.
Definition: random_func.cpp:33
static void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
Definition: random_func.hpp:44
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:25
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:23