1 /* Copyright (c) 2019-2020. 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(std::string filename)
24 std::ifstream file(filename);
29 XBT_WARN("Could not open %s and thus not save the RNG state.", filename.c_str());
34 bool Random::write_state(std::string filename)
36 std::ofstream file(filename);
41 XBT_WARN("Could not open %s and thus not read the RNG state.", filename.c_str());
46 int StdRandom::uniform_int(int min, int max)
48 std::uniform_int_distribution<> dist(min, max);
49 return dist(mt19937_gen);
52 double StdRandom::uniform_real(double min, double max)
54 std::uniform_real_distribution<> dist(min, max);
55 return dist(mt19937_gen);
58 double StdRandom::exponential(double lambda)
60 std::exponential_distribution<> dist(lambda);
61 return dist(mt19937_gen);
64 double StdRandom::normal(double mean, double sd)
66 std::normal_distribution<> dist(mean, sd);
67 return dist(mt19937_gen);
70 int XbtRandom::uniform_int(int min, int max)
72 unsigned long range = max - min + 1;
73 xbt_assert(min <= max,
74 "The minimum value for the uniform integer distribution must not be greater than the maximum value");
75 xbt_assert(range > 0, "Overflow in the uniform integer distribution, please use a smaller range.");
78 value = mt19937_gen();
79 } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range);
80 return value % range + min;
83 double XbtRandom::uniform_real(double min, double max)
85 // This reuses Boost's uniform real distribution ideas
86 constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min();
87 unsigned long numerator;
89 numerator = mt19937_gen() - decltype(mt19937_gen)::min();
90 } while (numerator == divisor);
91 return min + (max - min) * numerator / divisor;
94 double XbtRandom::exponential(double lambda)
96 return -1.0 / lambda * log(uniform_real(0.0, 1.0));
99 double XbtRandom::normal(double mean, double sd)
103 u1 = uniform_real(0.0, 1.0);
104 } while (u1 < std::numeric_limits<double>::min());
105 double u2 = uniform_real(0.0, 1.0);
106 double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
107 return z0 * sd + mean;
110 static std::unique_ptr<Random> default_random(new XbtRandom);
112 void set_implem_xbt()
114 default_random.reset(new XbtRandom);
116 void set_implem_std()
118 default_random.reset(new StdRandom);
121 void set_mersenne_seed(int seed)
123 default_random->set_seed(seed);
126 bool read_mersenne_state(std::string filename)
128 return default_random->read_state(filename);
131 bool write_mersenne_state(std::string filename)
133 return default_random->write_state(filename);
136 int uniform_int(int min, int max)
138 return default_random->uniform_int(min, max);
141 double uniform_real(double min, double max)
143 return default_random->uniform_real(min, max);
146 double exponential(double lambda)
148 return default_random->exponential(lambda);
151 double normal(double mean, double sd)
153 return default_random->normal(mean, sd);
156 } // namespace random
158 } // namespace simgrid