Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'xbt_random' into 'master'
[simgrid.git] / src / xbt / random.cpp
1 /* Copyright (c) 2019. 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 <random>
10
11 namespace simgrid {
12 namespace xbt {
13 namespace random {
14 std::mt19937 mt19937_gen;
15 xbt_random_method current_rng = XBT_RNG_xbt;
16
17 void use_xbt()
18 {
19   current_rng = XBT_RNG_xbt;
20 }
21 void use_std()
22 {
23   current_rng = XBT_RNG_std;
24 }
25
26 int uniform_int(int min, int max)
27 {
28   switch (current_rng) {
29     case XBT_RNG_xbt:
30       return xbt_uniform_int(min, max);
31     case XBT_RNG_std: {
32       std::uniform_int_distribution<> dist(min, max);
33       return dist(mt19937_gen);
34     }
35     default:
36       xbt_assert(false, "The uniform integer distribution is not yet supported for the current RNG.");
37   }
38 }
39
40 int xbt_uniform_int(int min, int max)
41 {
42   unsigned long range  = max - min + 1;
43   unsigned long value  = mt19937_gen();
44   xbt_assert(range > 0, "Overflow in the uniform integer distribution, please use a smaller range.");
45   xbt_assert(
46       min <= max,
47       "The maximum value for the uniform integer distribution must be greater than or equal to the minimum value");
48   return value % range + min;
49 }
50
51 double uniform_real(double min, double max)
52 {
53   switch (current_rng) {
54     case XBT_RNG_xbt:
55       return xbt_uniform_real(min, max);
56     case XBT_RNG_std: {
57       std::uniform_real_distribution<> dist(min, max);
58       return dist(mt19937_gen);
59     }
60     default:
61       xbt_assert(false, "The uniform real distribution is not yet supported for the current RNG.");
62   }
63 }
64
65 double xbt_uniform_real(double min, double max)
66 {
67   // This reuses Boost's uniform real distribution ideas
68   unsigned long numerator = mt19937_gen() - mt19937_gen.min();
69   unsigned long divisor   = mt19937_gen.max() - mt19937_gen.min();
70   return min + (max - min) * numerator / divisor;
71 }
72
73 double exponential(double lambda)
74 {
75   switch (current_rng) {
76     case XBT_RNG_xbt:
77       return xbt_exponential(lambda);
78     case XBT_RNG_std: {
79       std::exponential_distribution<> dist(lambda);
80       return dist(mt19937_gen);
81     }
82     default:
83       xbt_assert(false, "The exponential distribution is not yet supported for the current RNG.");
84   }
85 }
86
87 double xbt_exponential(double lambda)
88 {
89   return -1 / lambda * log(uniform_real(0, 1));
90 }
91
92 double normal(double mean, double sd)
93 {
94   switch (current_rng) {
95     case XBT_RNG_xbt:
96       return xbt_normal(mean, sd);
97     case XBT_RNG_std: {
98       std::normal_distribution<> dist(mean, sd);
99       return dist(mt19937_gen);
100     }
101     default:
102       xbt_assert(false, "The normal distribution is not yet supported for the curent RNG.");
103   }
104 }
105
106 double xbt_normal(double mean, double sd)
107 {
108   double u1 = 0;
109   while (u1 < std::numeric_limits<double>::min()) {
110     u1 = uniform_real(0, 1);
111   }
112   double u2 = uniform_real(0, 1);
113   double z0 = sqrt(-2.0 * log(u1)) * cos(2 * M_PI * u2);
114   return z0 * sd + mean;
115 }
116
117 void set_mersenne_seed(int seed)
118 {
119   mt19937_gen.seed(seed);
120 }
121
122 } // namespace random
123 } // namespace xbt
124 } // namespace simgrid