1 /* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "xbt/asserts.h"
12 #include <xbt/log.hpp>
13 #include <xbt/random.hpp>
15 XBT_LOG_EXTERNAL_CATEGORY(xbt);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_random, xbt, "Random");
22 bool Random::read_state(const std::string& filename)
24 std::ifstream file(filename);
28 XBT_WARN("Could not save the RNG state to file %s.", filename.c_str());
29 return not file.fail();
32 bool Random::write_state(const std::string& filename) const
34 std::ofstream file(filename);
38 XBT_WARN("Could not read the RNG state from file %s.", filename.c_str());
39 return not file.fail();
42 int StdRandom::uniform_int(int min, int max)
44 std::uniform_int_distribution<> dist(min, max);
45 return dist(mt19937_gen);
48 double StdRandom::uniform_real(double min, double max)
50 std::uniform_real_distribution<> dist(min, max);
51 return dist(mt19937_gen);
54 double StdRandom::exponential(double lambda)
56 std::exponential_distribution<> dist(lambda);
57 return dist(mt19937_gen);
60 double StdRandom::normal(double mean, double sd)
62 std::normal_distribution<> dist(mean, sd);
63 return dist(mt19937_gen);
66 int XbtRandom::uniform_int(int min, int max)
68 // The casts to unsigned are here to ensure that the value of range is correctly calculated, even when greater than
69 // INT_MAX. See the corresponding unit tests for examples.
70 unsigned long range = static_cast<unsigned>(max) - static_cast<unsigned>(min);
71 xbt_assert(min <= max,
72 "The minimum value for the uniform integer distribution must not be greater than the maximum value");
73 xbt_assert(range <= decltype(mt19937_gen)::max(),
74 "Overflow in the uniform integer distribution, please use a smaller range.");
75 if (range == decltype(mt19937_gen)::max())
76 return static_cast<int>(mt19937_gen() + min);
79 unsigned long limit = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range;
82 value = mt19937_gen();
83 } while (value >= limit);
84 return static_cast<int>(value % range + min);
87 double XbtRandom::uniform_real(double min, double max)
89 // This reuses Boost's uniform real distribution ideas
90 constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min();
91 unsigned long numerator;
93 numerator = mt19937_gen() - decltype(mt19937_gen)::min();
94 } while (numerator == divisor);
95 return min + (max - min) * static_cast<double>(numerator) / divisor;
98 double XbtRandom::exponential(double lambda)
100 return -1.0 / lambda * log(uniform_real(0.0, 1.0));
103 double XbtRandom::normal(double mean, double sd)
107 u1 = uniform_real(0.0, 1.0);
108 } while (u1 < std::numeric_limits<double>::min());
109 double u2 = uniform_real(0.0, 1.0);
110 double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
111 return z0 * sd + mean;
114 static std::unique_ptr<Random> default_random = std::make_unique<XbtRandom>();
116 void set_implem_xbt()
118 default_random = std::make_unique<XbtRandom>();
120 void set_implem_std()
122 default_random = std::make_unique<StdRandom>();
125 void set_mersenne_seed(int seed)
127 default_random->set_seed(seed);
130 bool read_mersenne_state(const std::string& filename)
132 return default_random->read_state(filename);
135 bool write_mersenne_state(const std::string& filename)
137 return default_random->write_state(filename);
140 int uniform_int(int min, int max)
142 return default_random->uniform_int(min, max);
145 double uniform_real(double min, double max)
147 return default_random->uniform_real(min, max);
150 double exponential(double lambda)
152 return default_random->exponential(lambda);
155 double normal(double mean, double sd)
157 return default_random->normal(mean, sd);
160 } // namespace random
162 } // namespace simgrid