Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Random.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_RANDOM_H
22 #define GF_RANDOM_H
23 
24 #include <cstdint>
25 #include <random>
26 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  /**
35  * @ingroup core
36  * @brief A random engine
37  *
38  * gf::Random is a wrapper around [C++11 standard random features](http://en.cppreference.com/w/cpp/numeric/random).
39  * It embeds a Mersenne Twister engine and provides several distributions
40  * above this engine.
41  *
42  */
43  class GF_API Random {
44  public:
45  /**
46  * @brief Default constructor with complex initialization
47  *
48  * This constructor initializes the Mersenne Twister thanks to a random
49  * device and a seed sequence. This ensures that the state of the engine
50  * is different for each instanciation. Choose this constructor if you
51  * need good statistical randomness.
52  */
53  Random();
54 
55  /**
56  * @brief Constructor with simple initialization
57  *
58  * This constructor initializes the Mersenne Twister with a single seed.
59  * This method is easy but not very good because it allows only @f$ 2^{32} @f$
60  * possible states. Choose this constructor if you need reproducible
61  * randomness as a same seed will always provide the same sequence.
62  *
63  * @param seed The seed for the engine
64  */
65  Random(std::uint_fast32_t seed)
66  : m_engine(seed)
67  {
68 
69  }
70 
71  /**
72  * @brief Compute an integer with a uniform distribution
73  *
74  * @param min The minimum value (inclusive)
75  * @param max The maximum value (inclusive)
76  * @return A value between `min` and `max`
77  */
78  template<typename T>
79  T computeUniformInteger(T min, T max) {
80  std::uniform_int_distribution<T> dist(min, max);
81  return dist(m_engine);
82  }
83 
84  /**
85  * @brief Compute a float with a uniform distribution
86  *
87  * @param min The minimum value (inclusive)
88  * @param max The maximum value (exclusive)
89  * @return A value between `min` and `max`
90  */
91  template<typename T>
92  T computeUniformFloat(T min, T max) {
93  std::uniform_real_distribution<T> dist(min, max);
94  return dist(m_engine);
95  }
96 
97  /**
98  * @brief Compute a float with a normal (Gaussian) distribution
99  *
100  * @param mean The mean of the distribution
101  * @param stddev The standard deviation of the distribution
102  * @return A value with a normal distribution
103  */
104  template<typename T>
105  T computeNormalFloat(T mean, T stddev) {
106  std::normal_distribution<T> dist(mean, stddev);
107  return dist(m_engine);
108  }
109 
110  /**
111  * @brief Compute a boolean with a Bernoulli distribution
112  *
113  * @param p The probability of true
114  * @return true with a probability of @f$ p @f$.
115  */
116  bool computeBernoulli(double p) {
117  std::bernoulli_distribution dist(p);
118  return dist(m_engine);
119  }
120 
121  /**
122  * @brief Get the underlying engine
123  *
124  * @return A reference to the engine
125  */
126  std::mt19937& getEngine() {
127  return m_engine;
128  }
129 
130  private:
131  std::mt19937 m_engine;
132  };
133 
134 #ifndef DOXYGEN_SHOULD_SKIP_THIS
135 }
136 #endif
137 }
138 
139 #endif // GF_RANDOM_H
A random engine.
Definition: Random.h:43
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:105
T computeUniformFloat(T min, T max)
Compute a float with a uniform distribution.
Definition: Random.h:92
Random(std::uint_fast32_t seed)
Constructor with simple initialization.
Definition: Random.h:65
Definition: Action.h:34
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:116
Random()
Default constructor with complex initialization.
std::mt19937 & getEngine()
Get the underlying engine.
Definition: Random.h:126
T computeUniformInteger(T min, T max)
Compute an integer with a uniform distribution.
Definition: Random.h:79
#define GF_API
Definition: Portability.h:35