SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandHelper.h
Go to the documentation of this file.
1 /****************************************************************************/
8 //
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
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>
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 initRandGlobal(MTRand* which = 0);
59 
61  static inline SUMOReal rand() {
63  }
64 
66  static inline SUMOReal rand(SUMOReal maxV) {
67  return maxV * rand();
68  }
69 
71  static inline SUMOReal rand(SUMOReal minV, SUMOReal maxV) {
72  return minV + (maxV - minV) * rand();
73  }
74 
76  static inline size_t rand(size_t maxV) {
77  return (size_t) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV - 1));
78  }
79 
81  static inline int rand(int maxV) {
83  }
84 
86  static inline int rand(int minV, int maxV) {
87  return minV + rand(maxV - minV);
88  }
89 
91  static inline SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand& rng = myRandomNumberGenerator) {
92  return (SUMOReal) rng.randNorm(mean, variance);
93  }
94 
96  template<class T>
97  static inline T
98  getRandomFrom(const std::vector<T>& v) {
99  assert(v.size() > 0);
100  return v[rand(v.size())];
101  }
102 
103 
104 protected:
107 
108 };
109 
110 #endif
111 
112 /****************************************************************************/
113 
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:61
static SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand &rng=myRandomNumberGenerator)
Access to a random number from a normal distribution.
Definition: RandHelper.h:91
static T getRandomFrom(const std::vector< T > &v)
Returns a random element from the given vector.
Definition: RandHelper.h:98
Utility functions for using a global, resetable random number generator.
Definition: RandHelper.h:52
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:81
static size_t rand(size_t maxV)
Returns a random integer in [0, maxV-1].
Definition: RandHelper.h:76
static SUMOReal rand(SUMOReal maxV)
Returns a random real number in [0, maxV)
Definition: RandHelper.h:66
unsigned long uint32
static SUMOReal rand(SUMOReal minV, SUMOReal maxV)
Returns a random real number in [minV, maxV)
Definition: RandHelper.h:71
uint32 randInt()
static MTRand myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:106
A storage for options typed value containers)
Definition: OptionsCont.h:108
double randExc()
#define SUMOReal
Definition: config.h:221
static int rand(int minV, int maxV)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:86