Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
RandHelper.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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>
26#include "StdDefs.h"
27#include "Translation.h"
28#include "RandHelper.h"
29
30
31// debug vehicle movement randomness (dawdling in finalizeSpeed)
32//#define DEBUG_FLAG1
33//#define DEBUG_RANDCALLS
34//#define DEBUG_RANDCALLS_PARALLEL
35
36
37// ===========================================================================
38// static member variables
39// ===========================================================================
41
42#ifdef DEBUG_RANDCALLS
43unsigned long long int myDebugIndex(7);
44std::string myDebugId("");
45#endif
46
47#ifdef DEBUG_RANDCALLS_PARALLEL
48#include <thread>
50std::map<std::thread::id, int> threadIndices;
51std::map<std::string, int> lastThreadIndex; // by rng
52#endif
53
54
55// ===========================================================================
56// member method definitions
57// ===========================================================================
58
59void
61 // registers random number options
62 oc.addOptionSubTopic("Random Number");
63
64 oc.doRegister("random", new Option_Bool(false));
65 oc.addSynonyme("random", "abs-rand", true);
66 oc.addDescription("random", "Random Number", TL("Initialises the random number generator with the current system time"));
67
68 oc.doRegister("seed", new Option_Integer(23423));
69 oc.addSynonyme("seed", "srand", true);
70 oc.addDescription("seed", "Random Number", TL("Initialises the random number generator with the given value"));
71}
72
73
74void
75RandHelper::initRand(SumoRNG* which, const bool random, const int seed) {
76 if (which == nullptr) {
78 }
79 if (random) {
80 which->setSeed((unsigned long)time(nullptr));
81 } else {
82 which->setSeed(seed);
83 }
84}
85
86
87void
90 initRand(which, oc.getBool("random"), oc.getInt("seed"));
91}
92
93int
95 if (which == nullptr) {
97 }
98 return which->origSeed;
99}
100
101double
103 if (rng == nullptr) {
105 }
106 const double res = double((*rng)() / 4294967296.0);
107 rng->count++;
108#ifdef DEBUG_RANDCALLS
109 if (rng->count == myDebugIndex
110 && (myDebugId == "" || rng->id == myDebugId)) {
111 std::cout << "DEBUG\n"; // for setting breakpoint
112 }
113 std::stringstream stream; // to reduce output interleaving from different threads
114#ifdef DEBUG_RANDCALLS_PARALLEL
115 auto threadID = std::this_thread::get_id();
116 if (threadIndices.count(threadID) == 0) {
117 const int tmp = threadIndices.size();
118 threadIndices[threadID] = tmp;
119 }
120 int threadIndex = threadIndices[threadID];
121 auto it = lastThreadIndex.find(rng->id);
122 if ((it == lastThreadIndex.end() || it->second != threadIndex)
123 && (myDebugId == "" || rng->id == myDebugId)) {
124 std::cout << "DEBUG rng " << rng->id << " change thread old=" << (it == lastThreadIndex.end() ? -1 : it->second) << " new=" << threadIndex << " (" << std::this_thread::get_id() << ")\n"; // for setting breakpoint
125 }
126 lastThreadIndex[rng->id] = threadIndex;
127 stream << "rng " << rng->id << " call=" << rng->count << " thread=" << threadIndex << " val=" << res << "\n";
128 OutputDevice::getDevice(rng->id) << stream.str();
129#else
130 stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
131 std::cout << stream.str();
132#endif
133#endif
134#ifdef DEBUG_FLAG1
135 if (gDebugFlag1) {
136 std::stringstream stream; // to reduce output interleaving from different threads
137 stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
138 std::cout << stream.str();
139 }
140#endif
141 return res;
142}
143
144double
145RandHelper::randNorm(double mean, double variance, SumoRNG* rng) {
146 // Polar method to avoid cosine
147 double u, q;
148 do {
149 u = rand(2.0, rng) - 1;
150 const double v = rand(2.0, rng) - 1;
151 q = u * u + v * v;
152 } while (q == 0.0 || q >= 1.0);
153 const double logRounded = ceil(log(q) * 1e14) / 1e14;
154 return mean + variance * u * sqrt(-2 * logRounded / q);
155}
156
157double
158RandHelper::randExp(double rate, SumoRNG* rng) {
159 double r = rand(rng);
160 while (r == 0) {
161 r = rand(rng);
162 }
163 return -log(r) / rate;
164}
165
166// template<class T>
167// void RandHelper::shuffle(const std::vector<T>& v) {
168// std::shuffle(v.begin(), v.end(), rng);
169// }
170
171/****************************************************************************/
#define TL(string)
Definition MsgHandler.h:304
bool gDebugFlag1
global utility flags for debugging
Definition StdDefs.cpp:44
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.
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)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
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.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
static SumoRNG myRandomNumberGenerator
the default random number generator to use
Definition RandHelper.h:311
static double randExp(double rate, SumoRNG *rng=nullptr)
Access to a random number from an exponential distribution.
static void initRand(SumoRNG *which=nullptr, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
static int getSeed(SumoRNG *which=nullptr)
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
static void insertRandOptions(OptionsCont &oc)
Initialises the given options container with random number options.
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
std::string id
Definition RandHelper.h:125
int origSeed
Definition RandHelper.h:124
unsigned long long int count
Definition RandHelper.h:123
void setSeed(int _seed)
Definition RandHelper.h:118