1 /* Copyright (c) 2019-2020. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_XBT_RANDOM_HPP
7 #define SIMGRID_XBT_RANDOM_HPP
19 /** A random number generator.
21 * It uses a std::mersenne_twister_engine (std::mt19937) and provides several distributions.
22 * This interface is implemented by StdRandom and XbtRandom.
24 class XBT_PUBLIC Random {
26 std::mt19937 mt19937_gen; // the random number engine
28 /** @brief Build a new random number generator with default seed */
30 /** @brief Build a new random number generator with given seed */
31 explicit Random(int seed) : mt19937_gen(seed) {}
33 virtual ~Random() = default;
36 * @brief Sets the seed of the Mersenne-Twister RNG
38 void set_seed(int seed) { mt19937_gen.seed(seed); }
41 * @brief Read the state of the Mersenne-Twister RNG from a file
43 void read_state(std::string filename)
45 std::ifstream file(filename);
50 * @brief Write the state of the Mersenne-Twister RNG to a file
52 void write_state(std::string filename)
54 std::ofstream file(filename);
59 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
61 * @param min Minimum value
62 * @param max Maximum value
64 virtual int uniform_int(int min, int max) = 0;
67 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
69 * @param min Minimum value
70 * @param max Maximum value
72 virtual double uniform_real(double min, double max) = 0;
75 * @brief Draws a real number according to the given exponential distribution
77 * @param lambda Parameter of the exponential law
79 virtual double exponential(double lambda) = 0;
82 * @brief Draws a real number according to the given normal distribution
84 * @param mean Mean of the normal distribution
85 * @param sd Standard deviation of the normal distribution
87 virtual double normal(double mean, double sd) = 0;
90 /** A random number generator using the C++ standard library.
92 * Caution: reproducibility is not guaranteed across different implementations.
94 class XBT_PUBLIC StdRandom : public Random {
96 StdRandom() = default;
97 explicit StdRandom(int seed) : Random(seed) {}
99 int uniform_int(int min, int max) override;
100 double uniform_real(double min, double max) override;
101 double exponential(double lambda) override;
102 double normal(double mean, double sd) override;
105 /** A reproducible random number generator.
107 * Uses our own implementation of distributions to ensure reproducibility.
109 class XBT_PUBLIC XbtRandom : public Random {
111 XbtRandom() = default;
112 explicit XbtRandom(int seed) : Random(seed) {}
114 int uniform_int(int min, int max) override;
115 double uniform_real(double min, double max) override;
116 double exponential(double lambda) override;
117 double normal(double mean, double sd) override;
121 * @brief Tells xbt/random to use the ad-hoc distribution implementation.
123 void set_implem_xbt();
126 * @brief Tells xbt/random to use the standard library distribution implementation.
128 void set_implem_std();
131 * @brief Sets the seed of the Mersenne-Twister RNG
133 void set_mersenne_seed(int);
136 * @brief Read the state of the Mersenne-Twister RNG from a file
138 void read_mersenne_state(std::string filename);
141 * @brief Write the state of the Mersenne-Twister RNG to a file
143 void write_mersenne_state(std::string filename);
146 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
148 * @param min Minimum value
149 * @param max Maximum value
151 int uniform_int(int min, int max);
154 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
156 * @param min Minimum value
157 * @param max Maximum value
159 double uniform_real(double min, double max);
162 * @brief Draws a real number according to the given exponential distribution
164 * @param lambda Parameter of the exponential law
166 double exponential(double lambda);
169 * @brief Draws a real number according to the given normal distribution
171 * @param mean Mean of the normal distribution
172 * @param sd Standard deviation of the normal distribution
174 double normal(double mean, double sd);
175 } // namespace random
177 } // namespace simgrid