1 /* Copyright (c) 2019-2023. 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
15 namespace simgrid::xbt::random {
17 /** A random number generator.
19 * It uses a std::mersenne_twister_engine (std::mt19937) and provides several distributions.
20 * This interface is implemented by StdRandom and XbtRandom.
22 class XBT_PUBLIC Random {
24 std::mt19937 mt19937_gen; // the random number engine
26 /** @brief Build a new random number generator with default seed */
28 /** @brief Build a new random number generator with given seed */
29 explicit Random(int seed) : mt19937_gen(seed) {}
31 virtual ~Random() = default;
34 * @brief Sets the seed of the Mersenne-Twister RNG
36 void set_seed(int seed) { mt19937_gen.seed(seed); }
39 * @brief Read the state of the Mersenne-Twister RNG from a file
41 bool read_state(const std::string& filename);
44 * @brief Write the state of the Mersenne-Twister RNG to a file
46 bool write_state(const std::string& filename) const;
49 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
51 * @param min Minimum value
52 * @param max Maximum value
54 virtual int uniform_int(int min, int max) = 0;
57 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
59 * @param min Minimum value
60 * @param max Maximum value
62 virtual double uniform_real(double min, double max) = 0;
65 * @brief Draws a real number according to the given exponential distribution
67 * @param lambda Parameter of the exponential law
69 virtual double exponential(double lambda) = 0;
72 * @brief Draws a real number according to the given normal distribution
74 * @param mean Mean of the normal distribution
75 * @param sd Standard deviation of the normal distribution
77 virtual double normal(double mean, double sd) = 0;
80 /** A random number generator using the C++ standard library.
82 * Caution: reproducibility is not guaranteed across different implementations.
84 class XBT_PUBLIC StdRandom : public Random {
88 int uniform_int(int min, int max) override;
89 double uniform_real(double min, double max) override;
90 double exponential(double lambda) override;
91 double normal(double mean, double sd) override;
94 /** A reproducible random number generator.
96 * Uses our own implementation of distributions to ensure reproducibility.
98 class XBT_PUBLIC XbtRandom : public Random {
100 using Random::Random;
102 int uniform_int(int min, int max) override;
103 double uniform_real(double min, double max) override;
104 double exponential(double lambda) override;
105 double normal(double mean, double sd) override;
109 * @brief Tells xbt/random to use the ad-hoc distribution implementation.
111 void set_implem_xbt();
114 * @brief Tells xbt/random to use the standard library distribution implementation.
116 void set_implem_std();
119 * @brief Sets the seed of the Mersenne-Twister RNG
121 void set_mersenne_seed(int);
124 * @brief Read the state of the Mersenne-Twister RNG from a file.
126 bool read_mersenne_state(const std::string& filename);
129 * @brief Write the state of the Mersenne-Twister RNG to a file.
131 bool write_mersenne_state(const std::string& filename);
134 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
136 * @param min Minimum value
137 * @param max Maximum value
139 int uniform_int(int min, int max);
142 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
144 * @param min Minimum value
145 * @param max Maximum value
147 double uniform_real(double min, double max);
150 * @brief Draws a real number according to the given exponential distribution
152 * @param lambda Parameter of the exponential law
154 double exponential(double lambda);
157 * @brief Draws a real number according to the given normal distribution
159 * @param mean Mean of the normal distribution
160 * @param sd Standard deviation of the normal distribution
162 double normal(double mean, double sd);
163 } // namespace simgrid::xbt::random