Random.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__RANDOM
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__RANDOM
19 
20 #include <Balau/Util/Impl/RandomImpl.hpp>
21 
22 namespace Balau::Util {
23 
25 
30 template <typename T, typename D> class RandomNumberGenerator final {
34  public: RandomNumberGenerator(T lower, T upper)
35  : generator(std::random_device()())
36  , distribution(D(lower, Impl::inclusivise(upper))) {}
37 
41  public: RandomNumberGenerator(T lower, T upper, unsigned int seed)
42  : generator(seed)
43  , distribution(D(lower, Impl::inclusivise(upper))) {}
44 
48  public: T operator () () {
49  return distribution(generator);
50  }
51 
53 
54  public: RandomNumberGenerator(const RandomNumberGenerator & copy) = delete;
55  public: RandomNumberGenerator & operator = (const RandomNumberGenerator & copy) = delete;
56 
57  private: std::mt19937 generator;
58  private: D distribution;
59 };
60 
62 
67 
72 
77 
82 
87 
92 
94 
99 
104 
106 
113 
120 
127 
134 
141 
148 
155 
162 
169 
176 
183 
190 
197 
204 
211 
218 
225 
232 
239 
240 } // namespace Balau::Util
241 
242 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__RANDOM
Utility functions.
STL namespace.
RandomNumberGenerator(T lower, T upper, unsigned int seed)
Create an explicitly seeded random number generator with a range of [lower, upper].
Definition: Random.hpp:41
T operator()()
Get the next random number.
Definition: Random.hpp:48
The random number generator implementation class.
Definition: Random.hpp:30
RandomNumberGenerator(T lower, T upper)
Create a randomly seeded random number generator with a range of [lower, upper].
Definition: Random.hpp:34