Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dd4b3db6b4017e8a9ac9553c51862b48dbfcfd46
[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/random.hpp"
7 #include "xbt/asserts.h"
8 #include <limits>
9 #include <memory>
10
11 namespace simgrid {
12 namespace xbt {
13 namespace random {
14
15 int StdRandom::uniform_int(int min, int max)
16 {
17   std::uniform_int_distribution<> dist(min, max);
18   return dist(mt19937_gen);
19 }
20
21 double StdRandom::uniform_real(double min, double max)
22 {
23   std::uniform_real_distribution<> dist(min, max);
24   return dist(mt19937_gen);
25 }
26
27 double StdRandom::exponential(double lambda)
28 {
29   std::exponential_distribution<> dist(lambda);
30   return dist(mt19937_gen);
31 }
32
33 double StdRandom::normal(double mean, double sd)
34 {
35   std::normal_distribution<> dist(mean, sd);
36   return dist(mt19937_gen);
37 }
38
39 int XbtRandom::uniform_int(int min, int max)
40 {
41   unsigned long range  = max - min + 1;
42   xbt_assert(min <= max,
43              "The minimum value for the uniform integer distribution must not be greater than the maximum value");
44   xbt_assert(range > 0, "Overflow in the uniform integer distribution, please use a smaller range.");
45   unsigned long value;
46   do {
47     value = mt19937_gen();
48   } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range);
49   return value % range + min;
50 }
51
52 double XbtRandom::uniform_real(double min, double max)
53 {
54   // This reuses Boost's uniform real distribution ideas
55   constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min();
56   unsigned long numerator;
57   do {
58     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
59   } while (numerator == divisor);
60   return min + (max - min) * numerator / divisor;
61 }
62
63 double XbtRandom::exponential(double lambda)
64 {
65   return -1.0 / lambda * log(uniform_real(0.0, 1.0));
66 }
67
68 double XbtRandom::normal(double mean, double sd)
69 {
70   double u1;
71   do {
72     u1 = uniform_real(0.0, 1.0);
73   } while (u1 < std::numeric_limits<double>::min());
74   double u2 = uniform_real(0.0, 1.0);
75   double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
76   return z0 * sd + mean;
77 }
78
79 static std::unique_ptr<Random> default_random(new XbtRandom);
80
81 void set_implem_xbt()
82 {
83   default_random.reset(new XbtRandom);
84 }
85 void set_implem_std()
86 {
87   default_random.reset(new StdRandom);
88 }
89
90 void set_mersenne_seed(int seed)
91 {
92   default_random->set_seed(seed);
93 }
94
95 int uniform_int(int min, int max)
96 {
97   return default_random->uniform_int(min, max);
98 }
99
100 double uniform_real(double min, double max)
101 {
102   return default_random->uniform_real(min, max);
103 }
104
105 double exponential(double lambda)
106 {
107   return default_random->exponential(lambda);
108 }
109
110 double normal(double mean, double sd)
111 {
112   return default_random->normal(mean, sd);
113 }
114
115 } // namespace random
116 } // namespace xbt
117 } // namespace simgrid