Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / xbt / random.cpp
1 /* Copyright (c) 2019-2022. 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   // 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);
77
78   ++range;
79   unsigned long limit = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range;
80   unsigned long value;
81   do {
82     value = mt19937_gen();
83   } while (value >= limit);
84   return static_cast<int>(value % range + min);
85 }
86
87 double XbtRandom::uniform_real(double min, double max)
88 {
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;
92   do {
93     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
94   } while (numerator == divisor);
95   return min + (max - min) * static_cast<double>(numerator) / divisor;
96 }
97
98 double XbtRandom::exponential(double lambda)
99 {
100   return -1.0 / lambda * log(uniform_real(0.0, 1.0));
101 }
102
103 double XbtRandom::normal(double mean, double sd)
104 {
105   double u1;
106   do {
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;
112 }
113
114 static std::unique_ptr<Random> default_random = std::make_unique<XbtRandom>();
115
116 void set_implem_xbt()
117 {
118   default_random = std::make_unique<XbtRandom>();
119 }
120 void set_implem_std()
121 {
122   default_random = std::make_unique<StdRandom>();
123 }
124
125 void set_mersenne_seed(int seed)
126 {
127   default_random->set_seed(seed);
128 }
129
130 bool read_mersenne_state(const std::string& filename)
131 {
132   return default_random->read_state(filename);
133 }
134
135 bool write_mersenne_state(const std::string& filename)
136 {
137   return default_random->write_state(filename);
138 }
139
140 int uniform_int(int min, int max)
141 {
142   return default_random->uniform_int(min, max);
143 }
144
145 double uniform_real(double min, double max)
146 {
147   return default_random->uniform_real(min, max);
148 }
149
150 double exponential(double lambda)
151 {
152   return default_random->exponential(lambda);
153 }
154
155 double normal(double mean, double sd)
156 {
157   return default_random->normal(mean, sd);
158 }
159
160 } // namespace random
161 } // namespace xbt
162 } // namespace simgrid