SUMO - Simulation of Urban MObility
RandHelper.h
Go to the documentation of this file.
1 /****************************************************************************/
9 //
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef RandHelper_h
23 #define RandHelper_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <vector>
37 #include <random>
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class OptionsCont;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
53 class RandHelper {
54 public:
56  static void insertRandOptions();
57 
59  static void initRand(std::mt19937* which = 0, const bool random = false, const int seed = 23423);
60 
62  static void initRandGlobal(std::mt19937* which = 0);
63 
65  static inline double rand(std::mt19937* rng = 0) {
66  if (rng == 0) {
68  }
69  return double((*rng)() / 4294967296.0);
70  }
71 
73  static inline double rand(double maxV, std::mt19937* rng = 0) {
74  return maxV * rand(rng);
75  }
76 
78  static inline double rand(double minV, double maxV, std::mt19937* rng = 0) {
79  return minV + (maxV - minV) * rand(rng);
80  }
81 
83  static inline int rand(int maxV, std::mt19937* rng = 0) {
84  if (rng == 0) {
86  }
87  unsigned int usedBits = maxV - 1;
88  usedBits |= usedBits >> 1;
89  usedBits |= usedBits >> 2;
90  usedBits |= usedBits >> 4;
91  usedBits |= usedBits >> 8;
92  usedBits |= usedBits >> 16;
93 
94  // Draw numbers until one is found in [0, maxV-1]
95  int result;
96  do {
97  result = (*rng)() & usedBits;
98  } while (result >= maxV);
99  return result;
100  }
101 
103  static inline int rand(int minV, int maxV, std::mt19937* rng = 0) {
104  return minV + rand(maxV - minV, rng);
105  }
106 
108  static inline long long int rand(long long int maxV, std::mt19937* rng = 0) {
109  if (maxV <= std::numeric_limits<int>::max()) {
110  return rand((int)maxV, rng);
111  }
112  if (rng == 0) {
114  }
115  unsigned long long int usedBits = maxV - 1;
116  usedBits |= usedBits >> 1;
117  usedBits |= usedBits >> 2;
118  usedBits |= usedBits >> 4;
119  usedBits |= usedBits >> 8;
120  usedBits |= usedBits >> 16;
121  usedBits |= usedBits >> 32;
122 
123  // Draw numbers until one is found in [0, maxV-1]
124  long long int result;
125  do {
126  result = (((unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits; // toss unused bits to shorten search
127  } while (result >= maxV);
128  return result;
129  }
130 
132  static inline long long int rand(long long int minV, long long int maxV, std::mt19937* rng = 0) {
133  return minV + rand(maxV - minV, rng);
134  }
135 
137  static inline double randNorm(double mean, double variance, std::mt19937* rng = 0) {
138  // Polar method to avoid cosine
139  double u, q;
140  do {
141  u = rand(2.0, rng) - 1;
142  const double v = rand(2.0, rng) - 1;
143  q = u * u + v * v;
144  } while (q == 0.0 || q >= 1.0);
145  return (double)(mean + variance * u * sqrt(-2 * log(q) / q));
146  }
147 
149  template<class T>
150  static inline const T&
151  getRandomFrom(const std::vector<T>& v, std::mt19937* rng = 0) {
152  assert(v.size() > 0);
153  return v[rand((int)v.size(), rng)];
154  }
155 
156 
157 protected:
159  static std::mt19937 myRandomNumberGenerator;
160 
161 };
162 
163 #endif
164 
165 /****************************************************************************/
static double rand(double minV, double maxV, std::mt19937 *rng=0)
Returns a random real number in [minV, maxV)
Definition: RandHelper.h:78
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:48
static double rand(double maxV, std::mt19937 *rng=0)
Returns a random real number in [0, maxV)
Definition: RandHelper.h:73
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:65
static int rand(int minV, int maxV, std::mt19937 *rng=0)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:103
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:108
Utility functions for using a global, resetable random number generator.
Definition: RandHelper.h:53
static int rand(int maxV, std::mt19937 *rng=0)
Returns a random integer in [0, maxV-1].
Definition: RandHelper.h:83
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:137
#define max(a, b)
Definition: polyfonts.c:65
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:132
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:159
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:64
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.
Definition: RandHelper.h:151
A storage for options typed value containers)
Definition: OptionsCont.h:99
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:77