annotate egglib/egglib-2.1.5/include/egglib-cpp/Random.hpp @ 13:734a3572c1d6 draft

Uploaded
author dereeper
date Tue, 08 Jan 2019 08:45:34 -0500
parents 420b57c3c185
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
1 /*
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
2 Copyright 2008,2009,2012 Stéphane De Mita, Mathieu Siol
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
3 Adapted from MStrat, developed by Charles-Edouard Coste,
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
4 Thomas M. Bataillon, Mathieu Cotisson, Guy Decoux, Chistophe Rozale,
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
5 Daniel J. Schoen and Jacques L. David.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
6
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
7 This file is part of the EggLib library.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
8
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
9 EggLib is free software: you can redistribute it and/or modify
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
11 the Free Software Foundation, either version 3 of the License, or
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
12 (at your option) any later version.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
13
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
14 EggLib is distributed in the hope that it will be useful,
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
17 GNU General Public License for more details.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
18
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
20 along with EggLib. If not, see <http://www.gnu.org/licenses/>.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
21 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
22
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
23 #ifndef EGGLIB_RANDOM_HPP
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
24 #define EGGLIB_RANDOM_HPP
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
25
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
26 namespace egglib {
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
27
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
28 /** \brief Pseudo-random number generator
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
29 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
30 * \ingroup core
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
31 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
32 * Random is a pseudo-random number generator, adapted from a part of MStrat,
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
33 * developed by Charles-Edouard Coste, Thomas M. Bataillon, Mathieu Cotisson,
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
34 * Guy Decoux, Chistophe Rozale, Daniel J. Schoen and Jacques L. David.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
35 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
36 * It uses two different seeds. By default, they are initialized to available
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
37 * arbitrary values. However, a given sequence can be repeated by passing the
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
38 * same two seeds.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
39 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
40 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
41 class Random {
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
42 public:
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
43 /** \brief Initializes using default seeds
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
44 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
45 * Uses the current system time and the memory address of the object as an attempt to generate unique sequences.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
46 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
47 Random();
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
48
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
49 /** \brief Initializes using given seeds
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
50 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
51 * This constructor can be used to reproduce a given sequence.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
52 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
53 Random(double seed1, double seed2);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
54
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
55 /** \brief Draws a number from an exponential distribution
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
56 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
57 * \param expectation the distribution mean (also 1/lambda
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
58 * where lambda is the rate parameter).
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
59 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
60 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
61 double erand(double expectation);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
62
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
63 /** \brief Draws an integer from a uniform distribution bound by 0 and max (max is not included)
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
64 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
65 * max is not included.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
66 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
67 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
68 unsigned int irand(unsigned int max);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
69
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
70 /** \brief Draws an integer from a Poisson distribution with parameter p
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
71 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
72 * The Poisson transformation algorithm was taken from (in French)
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
73 * http://www.u-picardie.fr/~cochard/IEM/demos/C107/C107_3.htm.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
74 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
75 unsigned int prand(double p);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
76
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
77 /** \brief Draws a number from a normal distribution of expectation 0 and variance 1
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
78 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
79 * The algorithm used is the polar form of the Box-Muller
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
80 * algorithm. \todo use the Ziggurat algorithm for the
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
81 * nrand() method of Random.
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
82 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
83 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
84 double nrand();
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
85
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
86 /** \brief Draws a number from a geometric law
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
87 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
88 * \param param the parameter of the law
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
89 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
90 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
91 unsigned int grand(double);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
92
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
93 /** \brief Draws a number from a uniform distribution between 0 and 1
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
94 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
95 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
96 double uniform();
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
97
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
98 /** \brief Gets the current value of the first seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
99 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
100 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
101 double seed1() const;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
102
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
103 /** \brief Gets the current value of the second seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
104 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
105 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
106 double seed2() const;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
107
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
108 /** \brief Sets the current value of the first seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
109 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
110 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
111 void seed1(double);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
112
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
113 /** \brief Sets the current value of the second seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
114 *
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
115 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
116 void seed2(double);
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
117
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
118 private:
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
119 // First seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
120 double _seed1;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
121
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
122 // Second seed
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
123 double _seed2;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
124
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
125 /* since the normal random generator draws two numbers at
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
126 * a time, one is cached and returned at any subsequent call
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
127 */
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
128 bool b_ncached;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
129 double v_ncached;
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
130
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
131 };
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
132 }
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
133
420b57c3c185 Uploaded
dereeper
parents:
diff changeset
134 #endif