OpenTTD Source  1.11.2
random_func.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef RANDOM_FUNC_HPP
11 #define RANDOM_FUNC_HPP
12 
13 #if defined(__APPLE__)
14  /* Apple already has Random declared */
15 # define Random OTTD_Random
16 #endif /* __APPLE__ */
17 
21 struct Randomizer {
23  uint32 state[2];
24 
25  uint32 Next();
26  uint32 Next(uint32 limit);
27  void SetSeed(uint32 seed);
28 };
29 extern Randomizer _random;
31 
34  Randomizer random;
35  Randomizer interactive_random;
36 };
37 
42 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
43 {
44  storage->random = _random;
45  storage->interactive_random = _interactive_random;
46 }
47 
52 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
53 {
54  _random = storage.random;
55  _interactive_random = storage.interactive_random;
56 }
57 
58 void SetRandomSeed(uint32 seed);
59 #ifdef RANDOM_DEBUG
60 # ifdef __APPLE__
61 # define OTTD_Random() DoRandom(__LINE__, __FILE__)
62 # else
63 # define Random() DoRandom(__LINE__, __FILE__)
64 # endif
65  uint32 DoRandom(int line, const char *file);
66 # define RandomRange(limit) DoRandomRange(limit, __LINE__, __FILE__)
67  uint32 DoRandomRange(uint32 limit, int line, const char *file);
68 #else
69  static inline uint32 Random()
70  {
71  return _random.Next();
72  }
73 
81  static inline uint32 RandomRange(uint32 limit)
82  {
83  return _random.Next(limit);
84  }
85 #endif
86 
87 static inline uint32 InteractiveRandom()
88 {
89  return _interactive_random.Next();
90 }
91 
92 static inline uint32 InteractiveRandomRange(uint32 limit)
93 {
94  return _interactive_random.Next(limit);
95 }
96 
112 static inline bool Chance16I(const uint a, const uint b, const uint32 r)
113 {
114  assert(b != 0);
115  return (((uint16)r * b + b / 2) >> 16) < a;
116 }
117 
128 #ifdef RANDOM_DEBUG
129 # define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
130 #else
131 static inline bool Chance16(const uint a, const uint b)
132 {
133  return Chance16I(a, b, Random());
134 }
135 #endif /* RANDOM_DEBUG */
136 
152 #ifdef RANDOM_DEBUG
153 # define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
154 #else
155 static inline bool Chance16R(const uint a, const uint b, uint32 &r)
156 {
157  r = Random();
158  return Chance16I(a, b, r);
159 }
160 #endif /* RANDOM_DEBUG */
161 
162 #endif /* RANDOM_FUNC_HPP */
Chance16
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Definition: random_func.hpp:131
Randomizer::Next
uint32 Next()
Generate the next pseudo random number.
Definition: random_func.cpp:31
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
SavedRandomSeeds
Stores the state of all random number generators.
Definition: random_func.hpp:33
SetRandomSeed
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
SaveRandomSeeds
static void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
Definition: random_func.hpp:42
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
Chance16I
static bool Chance16I(const uint a, const uint b, const uint32 r)
Checks if a given randomize-number is below a given probability.
Definition: random_func.hpp:112
RestoreRandomSeeds
static void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Definition: random_func.hpp:52
_interactive_random
Randomizer _interactive_random
Random used everywhere else, where it does not (directly) influence the game state.
Definition: random_func.cpp:25
Randomizer::state
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:21
Chance16R
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.
Definition: random_func.hpp:155
Randomizer::SetSeed
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:55