SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2015 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 size_t rand(size_t maxV) {
78  return (size_t) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV - 1));
79  }
80 
82  static inline int rand(int maxV) {
84  }
85 
87  static inline int rand(int minV, int maxV) {
88  return minV + rand(maxV - minV);
89  }
90 
92  static inline SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand* rng = 0) {
93  if (rng == 0) {
95  }
96  // Polar method to avoid cosine
97  double u, q;
98  do {
99  u = rng->randExc(2.0) - 1;
100  const double v = rng->randExc(2.0) - 1;
101  q = u * u + v * v;
102  } while (q == 0.0 || q >= 1.0);
103  return (SUMOReal)(mean + variance * u * sqrt(-2 * log(q) / q));
104  }
105 
107  template<class T>
108  static inline T
109  getRandomFrom(const std::vector<T>& v) {
110  assert(v.size() > 0);
111  return v[rand(v.size())];
112  }
113 
114 
115 protected:
118 
119 };
120 
121 #endif
122 
123 /****************************************************************************/
124 
static SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:92
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
static T getRandomFrom(const std::vector< T > &v)
Returns a random element from the given vector.
Definition: RandHelper.h:109
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:82
static size_t rand(size_t 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
uint32 randInt()
static MTRand myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:117
A storage for options typed value containers)
Definition: OptionsCont.h:108
double randExc()
#define SUMOReal
Definition: config.h:218
static int rand(int minV, int maxV)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:87