Eclipse SUMO - Simulation of Urban MObility
RandHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 //
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <ctime>
25 #include <utils/common/SysUtils.h>
26 #include "RandHelper.h"
27 
28 
29 // ===========================================================================
30 // static member variables
31 // ===========================================================================
33 #ifdef DEBUG_RANDCALLS
34 std::map<SumoRNG*, int> RandHelper::myRngId;
35 int RandHelper::myDebugIndex(7);
36 #endif
37 
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
42 void
45  // registers random number options
46  oc.addOptionSubTopic("Random Number");
47 
48  oc.doRegister("random", new Option_Bool(false));
49  oc.addSynonyme("random", "abs-rand", true);
50  oc.addDescription("random", "Random Number", "Initialises the random number generator with the current system time");
51 
52  oc.doRegister("seed", new Option_Integer(23423));
53  oc.addSynonyme("seed", "srand", true);
54  oc.addDescription("seed", "Random Number", "Initialises the random number generator with the given value");
55 }
56 
57 
58 void
59 RandHelper::initRand(SumoRNG* which, const bool random, const int seed) {
60  if (which == nullptr) {
61  which = &myRandomNumberGenerator;
62  }
63 #ifdef DEBUG_RANDCALLS
64  myRngId[which] = myRngId.size();
65 #endif
66  if (random) {
67  which->seed((unsigned long)time(nullptr));
68  } else {
69  which->seed(seed);
70  }
71 }
72 
73 
74 void
77  initRand(which, oc.getBool("random"), oc.getInt("seed"));
78 }
79 
80 
81 double
82 RandHelper::randNorm(double mean, double variance, SumoRNG* rng) {
83  // Polar method to avoid cosine
84  double u, q;
85  do {
86  u = rand(2.0, rng) - 1;
87  const double v = rand(2.0, rng) - 1;
88  q = u * u + v * v;
89  } while (q == 0.0 || q >= 1.0);
90  const double logRounded = ceil(log(q) * 1e14) / 1e14;
91  return mean + variance * u * sqrt(-2 * logRounded / q);
92 }
93 
94 
95 /****************************************************************************/
An integer-option.
Definition: Option.h:329
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static SumoRNG myRandomNumberGenerator
the default random number generator to use
Definition: RandHelper.h:244
static void initRand(SumoRNG *which=nullptr, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
Definition: RandHelper.cpp:59
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
Definition: RandHelper.cpp:82
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:119
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:75