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 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 {
88 StdRandom() = default;
89 explicit StdRandom(int seed) : Random(seed) {}
91 int uniform_int(int min, int max) override;
92 double uniform_real(double min, double max) override;
93 double exponential(double lambda) override;
94 double normal(double mean, double sd) override;
97 /** A reproducible random number generator.
99 * Uses our own implementation of distributions to ensure reproducibility.
101 class XBT_PUBLIC XbtRandom : public Random {
103 XbtRandom() = default;
104 explicit XbtRandom(int seed) : Random(seed) {}
106 int uniform_int(int min, int max) override;
107 double uniform_real(double min, double max) override;
108 double exponential(double lambda) override;
109 double normal(double mean, double sd) override;
113 * @brief Tells xbt/random to use the ad-hoc distribution implementation.
115 void set_implem_xbt();
118 * @brief Tells xbt/random to use the standard library distribution implementation.
120 void set_implem_std();
123 * @brief Sets the seed of the Mersenne-Twister RNG
125 void set_mersenne_seed(int);
128 * @brief Read the state of the Mersenne-Twister RNG from a file.
130 bool read_mersenne_state(const std::string& filename);
133 * @brief Write the state of the Mersenne-Twister RNG to a file.
135 bool write_mersenne_state(const std::string& filename);
138 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
140 * @param min Minimum value
141 * @param max Maximum value
143 int uniform_int(int min, int max);
146 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
148 * @param min Minimum value
149 * @param max Maximum value
151 double uniform_real(double min, double max);
154 * @brief Draws a real number according to the given exponential distribution
156 * @param lambda Parameter of the exponential law
158 double exponential(double lambda);
161 * @brief Draws a real number according to the given normal distribution
163 * @param mean Mean of the normal distribution
164 * @param sd Standard deviation of the normal distribution
166 double normal(double mean, double sd);
167 } // namespace random
169 } // namespace simgrid