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 /****************************************************************************/
9 //
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2005-2014 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 = myRandomNumberGenerator) {
93  return (SUMOReal) rng.randNorm(mean, variance);
94  }
95 
97  template<class T>
98  static inline T
99  getRandomFrom(const std::vector<T>& v) {
100  assert(v.size() > 0);
101  return v[rand(v.size())];
102  }
103 
104 
105 protected:
108 
109 };
110 
111 #endif
112 
113 /****************************************************************************/
114 
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 SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand &rng=myRandomNumberGenerator)
Access to a random number from a normal distribution.
Definition: RandHelper.h:92
static T getRandomFrom(const std::vector< T > &v)
Returns a random element from the given vector.
Definition: RandHelper.h:99
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:107
A storage for options typed value containers)
Definition: OptionsCont.h:108
double randExc()
#define SUMOReal
Definition: config.h:215
static int rand(int minV, int maxV)
Returns a random integer in [minV, maxV-1].
Definition: RandHelper.h:87