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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 //
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <ctime>
26 #include <utils/common/SysUtils.h>
27 #include "RandHelper.h"
28 
29 
30 // ===========================================================================
31 // static member variables
32 // ===========================================================================
34 #ifdef DEBUG_RANDCALLS
35 std::map<std::mt19937*, int> RandHelper::myCallCount;
36 std::map<std::mt19937*, int> RandHelper::myRngId;
37 int RandHelper::myDebugIndex(7);
38 #endif
39 
40 
41 // ===========================================================================
42 // member method definitions
43 // ===========================================================================
44 void
47  // registers random number options
48  oc.addOptionSubTopic("Random Number");
49 
50  oc.doRegister("random", new Option_Bool(false));
51  oc.addSynonyme("random", "abs-rand", true);
52  oc.addDescription("random", "Random Number", "Initialises the random number generator with the current system time");
53 
54  oc.doRegister("seed", new Option_Integer(23423));
55  oc.addSynonyme("seed", "srand", true);
56  oc.addDescription("seed", "Random Number", "Initialises the random number generator with the given value");
57 }
58 
59 
60 void
61 RandHelper::initRand(std::mt19937* which, const bool random, const int seed) {
62  if (which == nullptr) {
63  which = &myRandomNumberGenerator;
64  }
65 #ifdef DEBUG_RANDCALLS
66  myRngId[which] = myRngId.size();
67 #endif
68  if (random) {
69  which->seed((unsigned long)time(nullptr));
70  } else {
71  which->seed(seed);
72  }
73 }
74 
75 
76 void
77 RandHelper::initRandGlobal(std::mt19937* which) {
79  initRand(which, oc.getBool("random"), oc.getInt("seed"));
80 }
81 
82 
83 /****************************************************************************/
84 
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:215
Option_Bool
Definition: Option.h:538
OptionsCont.h
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
OptionsCont::addDescription
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Definition: OptionsCont.cpp:469
OptionsCont::addSynonyme
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:95
SysUtils.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:519
RandHelper::initRandGlobal
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:77
RandHelper::myRandomNumberGenerator
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:176
config.h
RandHelper::insertRandOptions
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:45
RandHelper.h
RandHelper::initRand
static void initRand(std::mt19937 *which=0, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
Definition: RandHelper.cpp:61
Option_Integer
An integer-option.
Definition: Option.h:331