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>
24#include "StdDefs.h"
25#include "RandHelper.h"
26
27
28// debug vehicle movement randomness (dawdling in finalizeSpeed)
29//#define DEBUG_FLAG1
30//#define DEBUG_RANDCALLS
31//#define DEBUG_RANDCALLS_PARALLEL
32
33
34// ===========================================================================
35// static member variables
36// ===========================================================================
38
39#ifdef DEBUG_RANDCALLS
40unsigned long long int myDebugIndex(7);
41std::string myDebugId("");
42#endif
43
44#ifdef DEBUG_RANDCALLS_PARALLEL
45#include <thread>
47std::map<std::thread::id, int> threadIndices;
48std::map<std::string, int> lastThreadIndex; // by rng
49#endif
50
51
52// ===========================================================================
53// member method definitions
54// ===========================================================================
55
56void
57RandHelper::initRand(SumoRNG* which, const bool random, const int seed) {
58 if (which == nullptr) {
60 }
61 if (random) {
62 which->setSeed((unsigned long)time(nullptr));
63 } else {
64 which->setSeed(seed);
65 }
66}
67
68
69int
71 if (which == nullptr) {
73 }
74 return which->origSeed;
75}
76
77double
79 if (rng == nullptr) {
81 }
82 const double res = double((*rng)() / 4294967296.0);
83 rng->count++;
84#ifdef DEBUG_RANDCALLS
85 if (rng->count == myDebugIndex
86 && (myDebugId == "" || rng->id == myDebugId)) {
87 std::cout << "DEBUG\n"; // for setting breakpoint
88 }
89 std::stringstream stream; // to reduce output interleaving from different threads
90#ifdef DEBUG_RANDCALLS_PARALLEL
91 auto threadID = std::this_thread::get_id();
92 if (threadIndices.count(threadID) == 0) {
93 const int tmp = threadIndices.size();
94 threadIndices[threadID] = tmp;
95 }
96 int threadIndex = threadIndices[threadID];
97 auto it = lastThreadIndex.find(rng->id);
98 if ((it == lastThreadIndex.end() || it->second != threadIndex)
99 && (myDebugId == "" || rng->id == myDebugId)) {
100 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
101 }
102 lastThreadIndex[rng->id] = threadIndex;
103 stream << "rng " << rng->id << " call=" << rng->count << " thread=" << threadIndex << " val=" << res << "\n";
104 OutputDevice::getDevice(rng->id) << stream.str();
105#else
106 stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
107 std::cout << stream.str();
108#endif
109#endif
110#ifdef DEBUG_FLAG1
111 if (gDebugFlag1) {
112 std::stringstream stream; // to reduce output interleaving from different threads
113 stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
114 std::cout << stream.str();
115 }
116#endif
117 return res;
118}
119
120double
121RandHelper::randNorm(double mean, double variance, SumoRNG* rng) {
122 // Polar method to avoid cosine
123 double u, q;
124 do {
125 u = rand(2.0, rng) - 1;
126 const double v = rand(2.0, rng) - 1;
127 q = u * u + v * v;
128 } while (q == 0.0 || q >= 1.0);
129 const double logRounded = ceil(log(q) * 1e14) / 1e14;
130 return mean + variance * u * sqrt(-2 * logRounded / q);
131}
132
133double
134RandHelper::randExp(double rate, SumoRNG* rng) {
135 double r = rand(rng);
136 while (r == 0) {
137 r = rand(rng);
138 }
139 return -log(r) / rate;
140}
141
142// template<class T>
143// void RandHelper::shuffle(const std::vector<T>& v) {
144// std::shuffle(v.begin(), v.end(), rng);
145// }
146
147/****************************************************************************/
bool gDebugFlag1
global utility flags for debugging
Definition StdDefs.cpp:44
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:299
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.
std::string id
Definition RandHelper.h:119
int origSeed
Definition RandHelper.h:118
unsigned long long int count
Definition RandHelper.h:117
void setSeed(int _seed)
Definition RandHelper.h:112