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