Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Now returns true if the file could be opened
[simgrid.git] / src / xbt / random.cpp
1 /* Copyright (c) 2019-2020. 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(std::string filename)
23 {
24   std::ifstream file(filename);
25   if (file) {
26     file >> mt19937_gen;
27     return true;
28   } else {
29     XBT_WARN("Could not open %s and thus not save the RNG state.", filename.c_str());
30     return false;
31   }
32 }
33
34 bool Random::write_state(std::string filename)
35 {
36   std::ofstream file(filename);
37   if (file) {
38     file << mt19937_gen;
39     return true;
40   } else {
41     XBT_WARN("Could not open %s and thus not read the RNG state.", filename.c_str());
42     return false;
43   }
44 }
45
46 int StdRandom::uniform_int(int min, int max)
47 {
48   std::uniform_int_distribution<> dist(min, max);
49   return dist(mt19937_gen);
50 }
51
52 double StdRandom::uniform_real(double min, double max)
53 {
54   std::uniform_real_distribution<> dist(min, max);
55   return dist(mt19937_gen);
56 }
57
58 double StdRandom::exponential(double lambda)
59 {
60   std::exponential_distribution<> dist(lambda);
61   return dist(mt19937_gen);
62 }
63
64 double StdRandom::normal(double mean, double sd)
65 {
66   std::normal_distribution<> dist(mean, sd);
67   return dist(mt19937_gen);
68 }
69
70 int XbtRandom::uniform_int(int min, int max)
71 {
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.");
76   unsigned long value;
77   do {
78     value = mt19937_gen();
79   } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range);
80   return value % range + min;
81 }
82
83 double XbtRandom::uniform_real(double min, double max)
84 {
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;
88   do {
89     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
90   } while (numerator == divisor);
91   return min + (max - min) * numerator / divisor;
92 }
93
94 double XbtRandom::exponential(double lambda)
95 {
96   return -1.0 / lambda * log(uniform_real(0.0, 1.0));
97 }
98
99 double XbtRandom::normal(double mean, double sd)
100 {
101   double u1;
102   do {
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;
108 }
109
110 static std::unique_ptr<Random> default_random(new XbtRandom);
111
112 void set_implem_xbt()
113 {
114   default_random.reset(new XbtRandom);
115 }
116 void set_implem_std()
117 {
118   default_random.reset(new StdRandom);
119 }
120
121 void set_mersenne_seed(int seed)
122 {
123   default_random->set_seed(seed);
124 }
125
126 bool read_mersenne_state(std::string filename)
127 {
128   return default_random->read_state(filename);
129 }
130
131 bool write_mersenne_state(std::string filename)
132 {
133   return default_random->write_state(filename);
134 }
135
136 int uniform_int(int min, int max)
137 {
138   return default_random->uniform_int(min, max);
139 }
140
141 double uniform_real(double min, double max)
142 {
143   return default_random->uniform_real(min, max);
144 }
145
146 double exponential(double lambda)
147 {
148   return default_random->exponential(lambda);
149 }
150
151 double normal(double mean, double sd)
152 {
153   return default_random->normal(mean, sd);
154 }
155
156 } // namespace random
157 } // namespace xbt
158 } // namespace simgrid