Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
449782a4f68fa7be26e3140e0c1b984c262fc169
[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   xbt_assert(range > 0, "Overflow in the uniform integer distribution, please use a smaller range.");
44   xbt_assert(
45       min <= max,
46       "The maximum value for the uniform integer distribution must be greater than or equal to the minimum value");
47   return min + (int)(range * xbt_uniform_real(0, 1));
48 }
49
50 double uniform_real(double min, double max)
51 {
52   switch (current_rng) {
53     case XBT_RNG_xbt:
54       return xbt_uniform_real(min, max);
55     case XBT_RNG_std: {
56       std::uniform_real_distribution<> dist(min, max);
57       return dist(mt19937_gen);
58     }
59     default:
60       xbt_assert(false, "The uniform real distribution is not yet supported for the current RNG.");
61   }
62 }
63
64 double xbt_uniform_real(double min, double max)
65 {
66   // This reuses Boost's uniform real distribution ideas
67   unsigned long numerator = mt19937_gen() - mt19937_gen.min();
68   unsigned long divisor   = mt19937_gen.max() - mt19937_gen.min();
69   return min + (max - min) * numerator / divisor;
70 }
71
72 double exponential(double lambda)
73 {
74   switch (current_rng) {
75     case XBT_RNG_xbt:
76       return xbt_exponential(lambda);
77     case XBT_RNG_std: {
78       std::exponential_distribution<> dist(lambda);
79       return dist(mt19937_gen);
80     }
81     default:
82       xbt_assert(false, "The exponential distribution is not yet supported for the current RNG.");
83   }
84 }
85
86 double xbt_exponential(double lambda)
87 {
88   return -1 / lambda * log(uniform_real(0, 1));
89 }
90
91 double normal(double mean, double sd)
92 {
93   switch (current_rng) {
94     case XBT_RNG_xbt:
95       return xbt_normal(mean, sd);
96     case XBT_RNG_std: {
97       std::normal_distribution<> dist(mean, sd);
98       return dist(mt19937_gen);
99     }
100     default:
101       xbt_assert(false, "The normal distribution is not yet supported for the curent RNG.");
102   }
103 }
104
105 double xbt_normal(double mean, double sd)
106 {
107   double u1 = 0;
108   while (u1 < std::numeric_limits<double>::min()) {
109     u1 = uniform_real(0, 1);
110   }
111   double u2 = uniform_real(0, 1);
112   double z0 = sqrt(-2.0 * log(u1)) * cos(2 * M_PI * u2);
113   return z0 * sd + mean;
114 }
115
116 void set_mersenne_seed(int seed)
117 {
118   mt19937_gen.seed(seed);
119 }
120
121 } // namespace random
122 } // namespace xbt
123 } // namespace simgrid