LCOV - code coverage report
Current view: top level - src/utils/common - RandHelper.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.1 % 35 34
Test Date: 2025-12-06 15:35:27 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2025 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              : /****************************************************************************/
      14              : /// @file    RandHelper.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @date    Tue, 29.05.2005
      18              : ///
      19              : //
      20              : /****************************************************************************/
      21              : #include <config.h>
      22              : 
      23              : #include <ctime>
      24              : #include <utils/options/OptionsCont.h>
      25              : #include <utils/common/SysUtils.h>
      26              : #include "StdDefs.h"
      27              : #include "RandHelper.h"
      28              : 
      29              : 
      30              : // debug vehicle movement randomness (dawdling in finalizeSpeed)
      31              : //#define DEBUG_FLAG1
      32              : //#define DEBUG_RANDCALLS
      33              : //#define DEBUG_RANDCALLS_PARALLEL
      34              : 
      35              : 
      36              : // ===========================================================================
      37              : // static member variables
      38              : // ===========================================================================
      39              : SumoRNG RandHelper::myRandomNumberGenerator("default");
      40              : 
      41              : #ifdef DEBUG_RANDCALLS
      42              : unsigned long long int myDebugIndex(7);
      43              : std::string myDebugId("");
      44              : #endif
      45              : 
      46              : #ifdef DEBUG_RANDCALLS_PARALLEL
      47              : #include <thread>
      48              : #include <utils/iodevices/OutputDevice.h>
      49              : std::map<std::thread::id, int> threadIndices;
      50              : std::map<std::string, int> lastThreadIndex; // by rng
      51              : #endif
      52              : 
      53              : 
      54              : // ===========================================================================
      55              : // member method definitions
      56              : // ===========================================================================
      57              : 
      58              : void
      59        46811 : RandHelper::insertRandOptions(OptionsCont& oc) {
      60              :     // registers random number options
      61        46811 :     oc.addOptionSubTopic("Random Number");
      62              : 
      63        46811 :     oc.doRegister("random", new Option_Bool(false));
      64        93622 :     oc.addSynonyme("random", "abs-rand", true);
      65        93622 :     oc.addDescription("random", "Random Number", TL("Initialises the random number generator with the current system time"));
      66              : 
      67        46811 :     oc.doRegister("seed", new Option_Integer(23423));
      68        93622 :     oc.addSynonyme("seed", "srand", true);
      69        93622 :     oc.addDescription("seed", "Random Number", TL("Initialises the random number generator with the given value"));
      70        46811 : }
      71              : 
      72              : 
      73              : void
      74      2785718 : RandHelper::initRand(SumoRNG* which, const bool random, const int seed) {
      75      2785718 :     if (which == nullptr) {
      76              :         which = &myRandomNumberGenerator;
      77              :     }
      78      2785718 :     if (random) {
      79            0 :         which->setSeed((unsigned long)time(nullptr));
      80              :     } else {
      81              :         which->setSeed(seed);
      82              :     }
      83      2785718 : }
      84              : 
      85              : 
      86              : void
      87       280501 : RandHelper::initRandGlobal(SumoRNG* which) {
      88       280501 :     OptionsCont& oc = OptionsCont::getOptions();
      89       561002 :     initRand(which, oc.getBool("random"), oc.getInt("seed"));
      90       280501 : }
      91              : 
      92              : int
      93      6196637 : RandHelper::getSeed(SumoRNG* which) {
      94      6196637 :     if (which == nullptr) {
      95              :         which = &myRandomNumberGenerator;
      96              :     }
      97      6196637 :     return which->origSeed;
      98              : }
      99              : 
     100              : double
     101    877539184 : RandHelper::rand(SumoRNG* rng) {
     102    877539184 :     if (rng == nullptr) {
     103              :         rng = &myRandomNumberGenerator;
     104              :     }
     105    877539184 :     const double res = double((*rng)() / 4294967296.0);
     106    877539184 :     rng->count++;
     107              : #ifdef DEBUG_RANDCALLS
     108              :     if (rng->count == myDebugIndex
     109              :             && (myDebugId == "" || rng->id == myDebugId)) {
     110              :         std::cout << "DEBUG\n"; // for setting breakpoint
     111              :     }
     112              :     std::stringstream stream; // to reduce output interleaving from different threads
     113              : #ifdef DEBUG_RANDCALLS_PARALLEL
     114              :     auto threadID = std::this_thread::get_id();
     115              :     if (threadIndices.count(threadID) == 0) {
     116              :         const int tmp = threadIndices.size();
     117              :         threadIndices[threadID] = tmp;
     118              :     }
     119              :     int threadIndex = threadIndices[threadID];
     120              :     auto it = lastThreadIndex.find(rng->id);
     121              :     if ((it == lastThreadIndex.end() || it->second != threadIndex)
     122              :             && (myDebugId == "" || rng->id == myDebugId)) {
     123              :         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
     124              :     }
     125              :     lastThreadIndex[rng->id] = threadIndex;
     126              :     stream << "rng " << rng->id << " call=" << rng->count << " thread=" << threadIndex << " val=" << res << "\n";
     127              :     OutputDevice::getDevice(rng->id) << stream.str();
     128              : #else
     129              :     stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
     130              :     std::cout << stream.str();
     131              : #endif
     132              : #endif
     133              : #ifdef DEBUG_FLAG1
     134              :     if (gDebugFlag1) {
     135              :         std::stringstream stream; // to reduce output interleaving from different threads
     136              :         stream << "rng " << rng->id << " call=" << rng->count << " val=" << res << "\n";
     137              :         std::cout << stream.str();
     138              :     }
     139              : #endif
     140    877539184 :     return res;
     141              : }
     142              : 
     143              : double
     144     38836101 : RandHelper::randNorm(double mean, double variance, SumoRNG* rng) {
     145              :     // Polar method to avoid cosine
     146              :     double u, q;
     147              :     do {
     148     49490496 :         u = rand(2.0, rng) - 1;
     149     49490496 :         const double v = rand(2.0, rng) - 1;
     150     49490496 :         q = u * u + v * v;
     151     49490496 :     } while (q == 0.0 || q >= 1.0);
     152     38836101 :     const double logRounded = ceil(log(q) * 1e14) / 1e14;
     153     38836101 :     return mean + variance * u * sqrt(-2 * logRounded / q);
     154              : }
     155              : 
     156              : double
     157       425655 : RandHelper::randExp(double rate, SumoRNG* rng) {
     158       425655 :     return -log(rand(rng)) / rate;
     159              : }
     160              : 
     161              : // template<class T>
     162              : // void RandHelper::shuffle(const std::vector<T>& v) {
     163              : //     std::shuffle(v.begin(), v.end(), rng);
     164              : // }
     165              : 
     166              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1