1 /* Copyright (c) 2019-2021. 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 bool read_state(const std::string& filename);
46 * @brief Write the state of the Mersenne-Twister RNG to a file
48 bool write_state(const std::string& filename) const;
51 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
53 * @param min Minimum value
54 * @param max Maximum value
56 virtual int uniform_int(int min, int max) = 0;
59 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
61 * @param min Minimum value
62 * @param max Maximum value
64 virtual double uniform_real(double min, double max) = 0;
67 * @brief Draws a real number according to the given exponential distribution
69 * @param lambda Parameter of the exponential law
71 virtual double exponential(double lambda) = 0;
74 * @brief Draws a real number according to the given normal distribution
76 * @param mean Mean of the normal distribution
77 * @param sd Standard deviation of the normal distribution
79 virtual double normal(double mean, double sd) = 0;
82 /** A random number generator using the C++ standard library.
84 * Caution: reproducibility is not guaranteed across different implementations.
86 class XBT_PUBLIC StdRandom : public Random {
90 int uniform_int(int min, int max) override;
91 double uniform_real(double min, double max) override;
92 double exponential(double lambda) override;
93 double normal(double mean, double sd) override;
96 /** A reproducible random number generator.
98 * Uses our own implementation of distributions to ensure reproducibility.
100 class XBT_PUBLIC XbtRandom : public Random {
102 using Random::Random;
104 int uniform_int(int min, int max) override;
105 double uniform_real(double min, double max) override;
106 double exponential(double lambda) override;
107 double normal(double mean, double sd) override;
111 * @brief Tells xbt/random to use the ad-hoc distribution implementation.
113 void set_implem_xbt();
116 * @brief Tells xbt/random to use the standard library distribution implementation.
118 void set_implem_std();
121 * @brief Sets the seed of the Mersenne-Twister RNG
123 void set_mersenne_seed(int);
126 * @brief Read the state of the Mersenne-Twister RNG from a file.
128 bool read_mersenne_state(const std::string& filename);
131 * @brief Write the state of the Mersenne-Twister RNG to a file.
133 bool write_mersenne_state(const std::string& filename);
136 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
138 * @param min Minimum value
139 * @param max Maximum value
141 int uniform_int(int min, int max);
144 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
146 * @param min Minimum value
147 * @param max Maximum value
149 double uniform_real(double min, double max);
152 * @brief Draws a real number according to the given exponential distribution
154 * @param lambda Parameter of the exponential law
156 double exponential(double lambda);
159 * @brief Draws a real number according to the given normal distribution
161 * @param mean Mean of the normal distribution
162 * @param sd Standard deviation of the normal distribution
164 double normal(double mean, double sd);
165 } // namespace random
167 } // namespace simgrid