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 /****************************************************************************/
16 //
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <ctime>
27 #include <utils/common/SysUtils.h>
28 #include "RandHelper.h"
29 
30 
31 // ===========================================================================
32 // static member variables
33 // ===========================================================================
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(std::mt19937* which, const bool random, const int seed) {
60  if (which == nullptr) {
61  which = &myRandomNumberGenerator;
62  }
63  if (random) {
64  which->seed((unsigned long)time(nullptr));
65  } else {
66  which->seed(seed);
67  }
68 }
69 
70 
71 void
72 RandHelper::initRandGlobal(std::mt19937* which) {
74  initRand(which, oc.getBool("random"), oc.getInt("seed"));
75 }
76 
77 
78 /****************************************************************************/
79 
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:216
Option_Bool
Definition: Option.h:540
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:223
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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:473
RandHelper::myCallCount
static int myCallCount
only used for debugging;
Definition: RandHelper.h:184
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:96
SysUtils.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
RandHelper::myDebugIndex
static int myDebugIndex
Definition: RandHelper.h:185
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:523
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:72
RandHelper::myRandomNumberGenerator
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:181
config.h
RandHelper::insertRandOptions
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
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:59
Option_Integer
An integer-option.
Definition: Option.h:333