SUMO - Simulation of Urban MObility
RandHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 //
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <ctime>
34 #include <utils/common/SysUtils.h>
35 #include "RandHelper.h"
36 
37 
38 // ===========================================================================
39 // static member variables
40 // ===========================================================================
42 
43 
44 // ===========================================================================
45 // member method definitions
46 // ===========================================================================
47 void
50  // registers random number options
51  oc.addOptionSubTopic("Random Number");
52 
53  oc.doRegister("random", new Option_Bool(false));
54  oc.addSynonyme("random", "abs-rand", true);
55  oc.addDescription("random", "Random Number", "Initialises the random number generator with the current system time");
56 
57  oc.doRegister("seed", new Option_Integer(23423));
58  oc.addSynonyme("seed", "srand", true);
59  oc.addDescription("seed", "Random Number", "Initialises the random number generator with the given value");
60 }
61 
62 
63 void
64 RandHelper::initRand(std::mt19937* which, const bool random, const int seed) {
65  if (which == 0) {
66  which = &myRandomNumberGenerator;
67  }
68  if (random) {
69  which->seed((unsigned long)time(0));
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 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:48
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:65
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
static std::mt19937 myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:159
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
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:64
An integer-option.
Definition: Option.h:313
A storage for options typed value containers)
Definition: OptionsCont.h:99
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
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)