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-2015 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 
33 #include <utils/common/SysUtils.h>
34 #include "RandHelper.h"
35 #include <ctime>
36 #include <cmath>
37 
38 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 
43 // ===========================================================================
44 // static member variables
45 // ===========================================================================
47 
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
52 void
55  // registers random number options
56  oc.addOptionSubTopic("Random Number");
57 
58  oc.doRegister("random", new Option_Bool(false));
59  oc.addSynonyme("random", "abs-rand", true);
60  oc.addDescription("random", "Random Number", "Initialises the random number generator with the current system time");
61 
62  oc.doRegister("seed", new Option_Integer(23423));
63  oc.addSynonyme("seed", "srand", true);
64  oc.addDescription("seed", "Random Number", "Initialises the random number generator with the given value");
65 }
66 
67 void
70  if (which == 0) {
71  which = &myRandomNumberGenerator;
72  }
73  if (oc.getBool("random")) {
74 #ifdef _MSC_VER
75  long s = myRandomNumberGenerator.hash(time(NULL), clock()) + SysUtils::getWindowsTicks();
76  unsigned int s2 = (unsigned int)(s & 0xffff) ^ (s >> 16);
77  which->seed(s2);
78 #else
79  which->seed();
80 #endif
81  } else {
82  which->seed(oc.getInt("seed"));
83  }
84 }
85 
86 
87 /****************************************************************************/
88 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
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:69
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static uint32 hash(time_t t, clock_t c)
An integer-option.
Definition: Option.h:309
void seed(const uint32 oneSeed)
static MTRand myRandomNumberGenerator
the random number generator to use
Definition: RandHelper.h:127
A storage for options typed value containers)
Definition: OptionsCont.h:108
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)