Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5621f5a345aa61e3c393eb4d26c46fee1114d279
[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   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(std::string filename)
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  = max - min + 1;
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 > 0, "Overflow in the uniform integer distribution, please use a smaller range.");
72   unsigned long value;
73   do {
74     value = mt19937_gen();
75   } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range);
76   return value % range + min;
77 }
78
79 double XbtRandom::uniform_real(double min, double max)
80 {
81   // This reuses Boost's uniform real distribution ideas
82   constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min();
83   unsigned long numerator;
84   do {
85     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
86   } while (numerator == divisor);
87   return min + (max - min) * numerator / divisor;
88 }
89
90 double XbtRandom::exponential(double lambda)
91 {
92   return -1.0 / lambda * log(uniform_real(0.0, 1.0));
93 }
94
95 double XbtRandom::normal(double mean, double sd)
96 {
97   double u1;
98   do {
99     u1 = uniform_real(0.0, 1.0);
100   } while (u1 < std::numeric_limits<double>::min());
101   double u2 = uniform_real(0.0, 1.0);
102   double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
103   return z0 * sd + mean;
104 }
105
106 static std::unique_ptr<Random> default_random(new XbtRandom);
107
108 void set_implem_xbt()
109 {
110   default_random.reset(new XbtRandom);
111 }
112 void set_implem_std()
113 {
114   default_random.reset(new StdRandom);
115 }
116
117 void set_mersenne_seed(int seed)
118 {
119   default_random->set_seed(seed);
120 }
121
122 bool read_mersenne_state(std::string filename)
123 {
124   return default_random->read_state(filename);
125 }
126
127 bool write_mersenne_state(std::string filename)
128 {
129   return default_random->write_state(filename);
130 }
131
132 int uniform_int(int min, int max)
133 {
134   return default_random->uniform_int(min, max);
135 }
136
137 double uniform_real(double min, double max)
138 {
139   return default_random->uniform_real(min, max);
140 }
141
142 double exponential(double lambda)
143 {
144   return default_random->exponential(lambda);
145 }
146
147 double normal(double mean, double sd)
148 {
149   return default_random->normal(mean, sd);
150 }
151
152 } // namespace random
153 } // namespace xbt
154 } // namespace simgrid