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-2020 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<std::mt19937*, int> RandHelper::myCallCount;
35 std::map<std::mt19937*, int> RandHelper::myRngId;
36 int RandHelper::myDebugIndex(7);
37 #endif
38 
39 
40 // ===========================================================================
41 // member method definitions
42 // ===========================================================================
43 void
46  // registers random number options
47  oc.addOptionSubTopic("Random Number");
48 
49  oc.doRegister("random", new Option_Bool(false));
50  oc.addSynonyme("random", "abs-rand", true);
51  oc.addDescription("random", "Random Number", "Initialises the random number generator with the current system time");
52 
53  oc.doRegister("seed", new Option_Integer(23423));
54  oc.addSynonyme("seed", "srand", true);
55  oc.addDescription("seed", "Random Number", "Initialises the random number generator with the given value");
56 }
57 
58 
59 void
60 RandHelper::initRand(std::mt19937* which, const bool random, const int seed) {
61  if (which == nullptr) {
62  which = &myRandomNumberGenerator;
63  }
64 #ifdef DEBUG_RANDCALLS
65  myRngId[which] = myRngId.size();
66 #endif
67  if (random) {
68  which->seed((unsigned long)time(nullptr));
69  } else {
70  which->seed(seed);
71  }
72 }
73 
74 
75 void
76 RandHelper::initRandGlobal(std::mt19937* which) {
78  initRand(which, oc.getBool("random"), oc.getInt("seed"));
79 }
80 
81 
82 /****************************************************************************/
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 void initRand(std::mt19937 *which=nullptr, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
Definition: RandHelper.cpp:60
static void initRandGlobal(std::mt19937 *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:76
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:44
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:174