SUMO - Simulation of Urban MObility
RandHelper.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 //
20 /****************************************************************************/
21 #ifndef RandHelper_h
22 #define RandHelper_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <vector>
36 #include <random>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class OptionsCont;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
52 class RandHelper {
53 public:
55  static void insertRandOptions();
56 
58  static void initRand(std::mt19937* which = 0, const bool random = false, const int seed = 23423);
59 
61  static void initRandGlobal(std::mt19937* which = 0);
62 
64  static inline double rand(std::mt19937* rng = 0) {
65  if (rng == 0) {
67  }
68  return double((*rng)() / 4294967296.0);
69  }
70 
72  static inline double rand(double maxV, std::mt19937* rng = 0) {
73  return maxV * rand(rng);
74  }
75 
77  static inline double rand(double minV, double maxV, std::mt19937* rng = 0) {
78  return minV + (maxV - minV) * rand(rng);
79  }
80 
82  static inline int rand(int maxV, std::mt19937* rng = 0) {
83  if (rng == 0) {
85  }
86  unsigned int usedBits = maxV - 1;
87  usedBits |= usedBits >> 1;
88  usedBits |= usedBits >> 2;
89  usedBits |= usedBits >> 4;
90  usedBits |= usedBits >> 8;
91  usedBits |= usedBits >> 16;
92 
93  // Draw numbers until one is found in [0, maxV-1]
94  int result;
95  do {
96  result = (*rng)() & usedBits;
97  } while (result >= maxV);
98  return result;
99  }
100 
102  static inline int rand(int minV, int maxV, std::mt19937* rng = 0) {
103  return minV + rand(maxV - minV, rng);
104  }
105 
107  static inline long long int rand(long long int maxV, std::mt19937* rng = 0) {
108  if (maxV <= std::numeric_limits<int>::max()) {
109  return rand((int)maxV, rng);
110  }
111  if (rng == 0) {
113  }
114  unsigned long long int usedBits = maxV - 1;
115  usedBits |= usedBits >> 1;
116  usedBits |= usedBits >> 2;
117  usedBits |= usedBits >> 4;
118  usedBits |= usedBits >> 8;
119  usedBits |= usedBits >> 16;
120  usedBits |= usedBits >> 32;
121 
122  // Draw numbers until one is found in [0, maxV-1]
123  long long int result;
124  do {
125  result = (((unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits; // toss unused bits to shorten search
126  } while (result >= maxV);
127  return result;
128  }
129 
131  static inline long long int rand(long long int minV, long long int maxV, std::mt19937* rng = 0) {
132  return minV + rand(maxV - minV, rng);
133  }
134 
136  static inline double randNorm(double mean, double variance, std::mt19937* rng = 0) {
137  // Polar method to avoid cosine
138  double u, q;
139  do {
140  u = rand(2.0, rng) - 1;
141  const double v = rand(2.0, rng) - 1;
142  q = u * u + v * v;
143  } while (q == 0.0 || q >= 1.0);
144  return (double)(mean + variance * u * sqrt(-2 * log(q) / q));
145  }
146 
148  template<class T>
149  static inline const T&
150  getRandomFrom(const std::vector<T>& v, std::mt19937* rng = 0) {
151  assert(v.size() > 0);
152  return v[rand((int)v.size(), rng)];
153  }
154 
155 
156 protected:
158  static std::mt19937 myRandomNumberGenerator;
159 
160 };
161 
162 #endif
163 
164 /****************************************************************************/
static double rand(double minV, double maxV, std::mt19937 *rng=0)
Returns a random real number in [minV, maxV)
Definition: RandHelper.h:77
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:47
static double rand(double maxV, std::mt19937 *rng=0)
Returns a random real number in [0, maxV)
Definition: RandHelper.h:72
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:64
static int rand(int minV, int maxV, std::mt19937 *rng=0)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:102
static long long int rand(long long int maxV, std::mt19937 *rng=0)
Returns a random 64 bit integer in [0, maxV-1].
Definition: RandHelper.h:107
Utility functions for using a global, resetable random number generator.
Definition: RandHelper.h:52
static int rand(int maxV, std::mt19937 *rng=0)
Returns a random integer in [0, maxV-1].
Definition: RandHelper.h:82
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:136
static long long int rand(long long int minV, long long int maxV, std::mt19937 *rng=0)
Returns a random 64 bit integer in [minV, maxV-1].
Definition: RandHelper.h:131
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:158
static void initRand(std::mt19937 *which=0, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
Definition: RandHelper.cpp:63
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.
Definition: RandHelper.h:150
A storage for options typed value containers)
Definition: OptionsCont.h:98
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:76