Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix XbtRandom::uniform_int() when used with large range.
[simgrid.git] / src / xbt / random.cpp
1 /* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved.               */
2
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. */
5
6 #include "xbt/asserts.h"
7 #include <fstream>
8 #include <iostream>
9 #include <limits>
10 #include <memory>
11 #include <string>
12 #include <xbt/log.hpp>
13 #include <xbt/random.hpp>
14
15 XBT_LOG_EXTERNAL_CATEGORY(xbt);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_random, xbt, "Random");
17
18 namespace simgrid {
19 namespace xbt {
20 namespace random {
21
22 bool Random::read_state(const std::string& filename)
23 {
24   std::ifstream file(filename);
25   file >> mt19937_gen;
26   file.close();
27   if (file.fail())
28     XBT_WARN("Could not save the RNG state to file %s.", filename.c_str());
29   return not file.fail();
30 }
31
32 bool Random::write_state(const std::string& filename) const
33 {
34   std::ofstream file(filename);
35   file << mt19937_gen;
36   file.close();
37   if (file.fail())
38     XBT_WARN("Could not read the RNG state from file %s.", filename.c_str());
39   return not file.fail();
40 }
41
42 int StdRandom::uniform_int(int min, int max)
43 {
44   std::uniform_int_distribution<> dist(min, max);
45   return dist(mt19937_gen);
46 }
47
48 double StdRandom::uniform_real(double min, double max)
49 {
50   std::uniform_real_distribution<> dist(min, max);
51   return dist(mt19937_gen);
52 }
53
54 double StdRandom::exponential(double lambda)
55 {
56   std::exponential_distribution<> dist(lambda);
57   return dist(mt19937_gen);
58 }
59
60 double StdRandom::normal(double mean, double sd)
61 {
62   std::normal_distribution<> dist(mean, sd);
63   return dist(mt19937_gen);
64 }
65
66 int XbtRandom::uniform_int(int min, int max)
67 {
68   unsigned long range = static_cast<unsigned>(max) - static_cast<unsigned>(min);
69   xbt_assert(min <= max,
70              "The minimum value for the uniform integer distribution must not be greater than the maximum value");
71   xbt_assert(range <= decltype(mt19937_gen)::max(),
72              "Overflow in the uniform integer distribution, please use a smaller range.");
73   if (range == decltype(mt19937_gen)::max())
74     return static_cast<int>(mt19937_gen() + min);
75
76   ++range;
77   unsigned long limit = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range;
78   unsigned long value;
79   do {
80     value = mt19937_gen();
81   } while (value >= limit);
82   return static_cast<int>(value % range + min);
83 }
84
85 double XbtRandom::uniform_real(double min, double max)
86 {
87   // This reuses Boost's uniform real distribution ideas
88   constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min();
89   unsigned long numerator;
90   do {
91     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
92   } while (numerator == divisor);
93   return min + (max - min) * static_cast<double>(numerator) / divisor;
94 }
95
96 double XbtRandom::exponential(double lambda)
97 {
98   return -1.0 / lambda * log(uniform_real(0.0, 1.0));
99 }
100
101 double XbtRandom::normal(double mean, double sd)
102 {
103   double u1;
104   do {
105     u1 = uniform_real(0.0, 1.0);
106   } while (u1 < std::numeric_limits<double>::min());
107   double u2 = uniform_real(0.0, 1.0);
108   double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
109   return z0 * sd + mean;
110 }
111
112 static std::unique_ptr<Random> default_random = std::make_unique<XbtRandom>();
113
114 void set_implem_xbt()
115 {
116   default_random = std::make_unique<XbtRandom>();
117 }
118 void set_implem_std()
119 {
120   default_random = std::make_unique<StdRandom>();
121 }
122
123 void set_mersenne_seed(int seed)
124 {
125   default_random->set_seed(seed);
126 }
127
128 bool read_mersenne_state(const std::string& filename)
129 {
130   return default_random->read_state(filename);
131 }
132
133 bool write_mersenne_state(const std::string& filename)
134 {
135   return default_random->write_state(filename);
136 }
137
138 int uniform_int(int min, int max)
139 {
140   return default_random->uniform_int(min, max);
141 }
142
143 double uniform_real(double min, double max)
144 {
145   return default_random->uniform_real(min, max);
146 }
147
148 double exponential(double lambda)
149 {
150   return default_random->exponential(lambda);
151 }
152
153 double normal(double mean, double sd)
154 {
155   return default_random->normal(mean, sd);
156 }
157
158 } // namespace random
159 } // namespace xbt
160 } // namespace simgrid