Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
random.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/random/mersenne_twister.hpp> // for mt19937
4 #include <boost/random/uniform_int_distribution.hpp> // for uniform_int_di...
5 #include <boost/random/uniform_real_distribution.hpp> // for uniform_real_d...
6 #include <boost/uuid/detail/seed_rng.hpp> // for seed
7 #include <limits> // for numeric_limits
8 #include <type_traits> // for conditional
9 
10 namespace inexor {
11 namespace util {
12 
13 namespace random {
15  typedef boost::random::mt19937 rng_engine;
16 
19  template<typename T>
20  class auto_seeded : public T {
21  public:
25  boost::uuids::detail::seed(*this);
26  }
27  };
28 
31  extern thread_local auto_seeded<rng_engine> generator;
32 
35  extern thread_local rng_engine deterministic_generator;
36 
38  typedef rng_engine::result_type seed_t;
39 
46  template<typename T>
48  : public boost::random::uniform_int_distribution<T> {
49  public:
51  T min = 0
53  : boost::random::uniform_int_distribution<T>(
54  min, max - 1) {}
55  };
56 
64  template<typename T>
67  boost::random::uniform_real_distribution<T>,
68  uniform_open_range_int_distribution<T>
69  >::type;
70 
87  template<typename T>
88  T rnd(const T Rmin, const T Rmax) {
89  // TODO. This could be expensive
91  return dist(generator);
92  }
93 
97  template<typename T>
98  T rnd(const T Rmax) {
99  return rnd(T(0), Rmax);
100  }
101 
108  template<typename T>
109  T rnd_raw() {
112  }
113 
114 
123  template<typename T>
124  T deterministic_rnd(const seed_t seed,
125  const T Rmin, const T Rmax) {
126  // TODO: Use a lookup table/cache?
128  deterministic_generator.seed(seed);
129  return dist(deterministic_generator);
130  }
131 
137  template<typename T>
138  T deterministic_rnd(const seed_t seed, const T Rmax) {
139  return deterministic_rnd(seed, T(0), Rmax);
140  }
141 }
142 
143 // Export to inexor::util
144 using random::rnd;
145 using random::rnd_raw;
147 
148 }
149 }
const T & max(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:224
auto_seeded()
The default constructor automatically generates secure seeds.
Definition: random.hpp:24
boost::random::mt19937 rng_engine
The default random engine for inexor.
Definition: random.hpp:15
thread_local rng_engine deterministic_generator
The default rng for the deterministic_rng functions; this will be reseeded every time they're called...
Definition: random.cpp:8
uniform_open_range_int_distribution(T min=0, T max=std::numeric_limits< T >::max)
Definition: random.hpp:50
It is like the boost uniform distribution, but while that uses an closed distribution [min; max]...
Definition: random.hpp:47
const T & min(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:210
thread_local auto_seeded< rng_engine > generator
The default rng, seeded with a time stamp from the best clock availabl (std::chrono::high_resolution_...
Definition: random.cpp:7
rng_engine::result_type seed_t
The type of the seed the deterministic_generator expects.
Definition: random.hpp:38
T rnd(const T Rmin, const T Rmax)
Generate a random number between Rmin and Rmax.
Definition: random.hpp:88
typename std::conditional< std::is_floating_point< T >::value, boost::random::uniform_real_distribution< T >, uniform_open_range_int_distribution< T > >::type uniform_generic_open_range_distribution
A generic uniform random number distribution that works on both floating point and integers...
Definition: random.hpp:69
T deterministic_rnd(const seed_t seed, const T Rmin, const T Rmax)
Generate a deterministic random number between Rmin and Rmax with a specific seed.
Definition: random.hpp:124
GLsizei GLboolean const GLfloat * value
Definition: glexts.hpp:57
Wrapper around any RNG Generator that seeds it automatically, by calling boost::uuids::detail::seed.
Definition: random.hpp:20
T rnd_raw()
Generate any random number.
Definition: random.hpp:109