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-2016 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>
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 initRandGlobal(MTRand* which = 0);
60 
62  static inline SUMOReal rand() {
64  }
65 
67  static inline SUMOReal rand(SUMOReal maxV) {
68  return maxV * rand();
69  }
70 
72  static inline SUMOReal rand(SUMOReal minV, SUMOReal maxV) {
73  return minV + (maxV - minV) * rand();
74  }
75 
77  static inline int rand(int maxV) {
79  }
80 
82  static inline int rand(int minV, int maxV) {
83  return minV + rand(maxV - minV);
84  }
85 
87  static inline long long int rand(long long int maxV) {
88  return (long long int) RandHelper::myRandomNumberGenerator.randInt64((unsigned long long int)(maxV - 1));
89  }
90 
92  static inline long long int rand(long long int minV, long long int maxV) {
93  return minV + rand(maxV - minV);
94  }
95 
97  static inline SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand* rng = 0) {
98  if (rng == 0) {
100  }
101  // Polar method to avoid cosine
102  double u, q;
103  do {
104  u = rng->randExc(2.0) - 1;
105  const double v = rng->randExc(2.0) - 1;
106  q = u * u + v * v;
107  } while (q == 0.0 || q >= 1.0);
108  return (SUMOReal)(mean + variance * u * sqrt(-2 * log(q) / q));
109  }
110 
112  template<class T>
113  static inline const T&
114  getRandomFrom(const std::vector<T>& v) {
115  assert(v.size() > 0);
116  return v[rand((int)v.size())];
117  }
118 
119 
120 protected:
123 
124 };
125 
126 #endif
127 
128 /****************************************************************************/
129 
static const T & getRandomFrom(const std::vector< T > &v)
Returns a random element from the given vector.
Definition: RandHelper.h:114
static SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:97
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
static long long int rand(long long int maxV)
Returns a random 64 bit integer in [0, maxV-1].
Definition: RandHelper.h:87
static long long int rand(long long int minV, long long int maxV)
Returns a random 64 bit integer in [minV, maxV-1].
Definition: RandHelper.h:92
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
Utility functions for using a global, resetable random number generator.
Definition: RandHelper.h:53
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
static int rand(int maxV)
Returns a random integer in [0, maxV-1].
Definition: RandHelper.h:77
static SUMOReal rand(SUMOReal maxV)
Returns a random real number in [0, maxV)
Definition: RandHelper.h:67
unsigned long uint32
static SUMOReal rand(SUMOReal minV, SUMOReal maxV)
Returns a random real number in [minV, maxV)
Definition: RandHelper.h:72
uint64 randInt64(const uint64 &n)
uint32 randInt()
static MTRand myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:122
A storage for options typed value containers)
Definition: OptionsCont.h:99
double randExc()
#define SUMOReal
Definition: config.h:213
static int rand(int minV, int maxV)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:82