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
16 /** A random number generator.
18 * It uses a std::mersenne_twister_engine (std::mt19937) and provides several distributions.
19 * This interface is implemented by StdRandom and XbtRandom.
21 class XBT_PUBLIC Random {
23 std::mt19937 mt19937_gen; // the random number engine
25 /** @brief Build a new random number generator with default seed */
27 /** @brief Build a new random number generator with given seed */
28 explicit Random(int seed) : mt19937_gen(seed) {}
30 virtual ~Random() = default;
33 * @brief Sets the seed of the Mersenne-Twister RNG
35 void set_seed(int seed) { mt19937_gen.seed(seed); }
38 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
40 * @param min Minimum value
41 * @param max Maximum value
43 virtual int uniform_int(int min, int max) = 0;
46 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
48 * @param min Minimum value
49 * @param max Maximum value
51 virtual double uniform_real(double min, double max) = 0;
54 * @brief Draws a real number according to the given exponential distribution
56 * @param lambda Parameter of the exponential law
58 virtual double exponential(double lambda) = 0;
61 * @brief Draws a real number according to the given normal distribution
63 * @param mean Mean of the normal distribution
64 * @param sd Standard deviation of the normal distribution
66 virtual double normal(double mean, double sd) = 0;
69 /** A random number generator using the C++ standard library.
71 * Caution: reproducibility is not guaranteed across different implementations.
73 class XBT_PUBLIC StdRandom : public Random {
75 StdRandom() = default;
76 explicit StdRandom(int seed) : Random(seed) {}
78 int uniform_int(int min, int max) override;
79 double uniform_real(double min, double max) override;
80 double exponential(double lambda) override;
81 double normal(double mean, double sd) override;
84 /** A reproducible random number generator.
86 * Uses our own implementation of distributions to ensure reproducibility.
88 class XBT_PUBLIC XbtRandom : public Random {
90 XbtRandom() = default;
91 explicit XbtRandom(int seed) : Random(seed) {}
93 int uniform_int(int min, int max) override;
94 double uniform_real(double min, double max) override;
95 double exponential(double lambda) override;
96 double normal(double mean, double sd) override;
100 * @brief Tells xbt/random to use the ad-hoc distribution implementation.
102 void set_implem_xbt();
105 * @brief Tells xbt/random to use the standard library distribution implementation.
107 void set_implem_std();
110 * @brief Sets the seed of the Mersenne-Twister RNG
112 void set_mersenne_seed(int);
115 * @brief Draws an integer number uniformly in range [min, max] (min and max included)
117 * @param min Minimum value
118 * @param max Maximum value
120 int uniform_int(int min, int max);
123 * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded)
125 * @param min Minimum value
126 * @param max Maximum value
128 double uniform_real(double min, double max);
131 * @brief Draws a real number according to the given exponential distribution
133 * @param lambda Parameter of the exponential law
135 double exponential(double lambda);
138 * @brief Draws a real number according to the given normal distribution
140 * @param mean Mean of the normal distribution
141 * @param sd Standard deviation of the normal distribution
143 double normal(double mean, double sd);
144 } // namespace random
146 } // namespace simgrid