#include <lot.h>
Public Types | |
| enum | { RAN_POL = 1, RAN_KNU, RAN_MT } |
| enum | { PRECISE = 100, FAST } |
| enum | { OPEN = 1000, ZERO, ZERO_ONE } |
Public Member Functions | |
| lot (int type=RAN_MT, int gType=ZERO) | |
| ~lot (void) | |
| void | set_generator (int type, int gType) |
| long | seed (void) |
| long | init_seed (void) |
| void | randomize (int spin=100) |
| void | set_seed (long s) |
| void | dememorize (int spin=100) |
| void | ran_start (long seed) |
| void | ranf_start (long seed) |
| int | random_int (int) |
| long | random_long (long maxval=Util::int_max) |
| long | ran_knu (void) |
| double | uniform (void) |
| void | ran_array (std::vector< long > &x, int n) |
| void | ranf_array (std::vector< double > &aa, int n) |
| void | MT_sgenrand (long seed) |
| void | MT_init_by_array (unsigned long *init_key, int key_length) |
| void | MT_R_initialize (int seed) |
| unsigned long | MT_genrand_int (void) |
| double | MT_genrand (void) |
| double | MT_genrand_with_zero (void) |
| double | MT_genrand_with_zero_one (void) |
| bool | Set_MT (int type) |
| double | beta (double aa, double bb) |
| int | binom (double nin, double pp) |
| double | cauchy (double l, double s) |
| double | chisq (double n) |
| std::vector< double > | dirichlet (std::vector< double > c) |
| double | expon (void) |
| double | exponential (const double lambda) |
| double | f (double m, double n) |
| double | gamma (double a, double scale=1.0) |
| double | geom (double p) |
| int | hypergeom (int nn1, int nn2, int kk) |
| double | igamma (const double a, const double s=1.0) |
| double | lnorm (double logmean, double logsd) |
| double | logis (double location, double scale) |
| std::vector< int > | multinom (unsigned n, const std::vector< double > &p) |
| double | nbinom (double n, double p) |
| int | poisson (double mu) |
| double | norm (const double mu, const double sd) |
| double | snorm (void) |
| double | t (double df) |
| double | weibull (double shape, double scale) |
| double | get_ran_u_0 (void) const |
This class provides a series of random number generators. Three different uniform random number generators are provided. By default the Mersenne twister is used. Sources are acknowledged in the source code by including copyright information (where appropriate).
It is given the name "lot" because the noun lot is defined as "an object used in deciding something by chance" according to The New Merriam-Webster Dictionary,
To seed the random number generator with a specific value (useful for debugging stochastic simulations), do the following:
You can, of course pick any number you like.
Definition at line 67 of file lot.h.
| anonymous enum |
| anonymous enum |
| anonymous enum |
| lot::lot | ( | int | type = RAN_MT, |
|
| int | gType = ZERO | |||
| ) |
Default constructor
Uses Mersenne twister on [0,1) by default and selects integer implementation of Knuth generator by default, using long -> double conversion for uniform on [0,1) instead of direct calculations with floating point
| type | RAN_POL (Lewis), RAN_KNU (Knuth), RAN_MT (Mersenne Twister) | |
| gType | RAN_KNU: PRECISE (floating point), FAST (long -> double) FAST is default because PRECISE != WITH_ZERO RAN_MT: OPEN (0,1), WITH_ZERO [0,1), ZERO_ONE [0,1] |
Definition at line 236 of file lot.cpp.
References set_generator().
| lot::~lot | ( | void | ) |
| void lot::set_generator | ( | int | type, | |
| int | gType | |||
| ) |
Set uniform RNG type
| type | RAN_POL, RAN_KNU, or RAN_MT | |
| gType | PRECISE or FAST (RAN_KNU) DEFAULT, WITH_ZERO, or ZERO_ONE (RAN_MT) |
Definition at line 259 of file lot.cpp.
References MT_genrand(), MT_sgenrand(), PRECISE, RAN_KNU, RAN_MT, RAN_POL, ran_start(), randomize(), ranf_start(), and Set_MT().
Referenced by lot().
| void lot::randomize | ( | int | spin = 100 |
) |
Initializes RAN_POL
| spin | (default 100) passed to dememorize() |
Definition at line 317 of file lot.cpp.
References dememorize().
Referenced by set_generator().
| void lot::set_seed | ( | long | s | ) |
Initialize RNG with known seed
| s | Seed |
Definition at line 328 of file lot.cpp.
References MT_sgenrand(), RAN_KNU, RAN_MT, RAN_POL, ran_start(), and ranf_start().
| void lot::dememorize | ( | int | spin = 100 |
) |
Used with RAN_POL to "warm up" generator
| spin | number of preliminary calls to uniform() |
Definition at line 304 of file lot.cpp.
References uniform().
Referenced by randomize().
| void lot::ran_start | ( | long | seed | ) |
Initialize the Knuth long RNG
Definition at line 567 of file lot.cpp.
References ran_array(), and t().
Referenced by set_generator(), and set_seed().
| void lot::ranf_start | ( | long | seed | ) |
Initialize the Knuth double RNG
Definition at line 443 of file lot.cpp.
References ranf_array(), and t().
Referenced by set_generator(), and set_seed().
| int lot::random_int | ( | int | maxval | ) |
| long lot::random_long | ( | long | maxval = Util::int_max |
) |
| long lot::ran_knu | ( | void | ) | [inline] |
Uniform random integer in [0, Util::int_max-1] with RAN_KNU
| double lot::uniform | ( | void | ) | [inline] |
Uniform random number
RAN_POL & RAN_KNU produce uniform on [0,1) RAN_MT produces uniform on [0,1) by default RAN_MT can produce uniform on (0,1) or [0,1]
Definition at line 126 of file lot.h.
Referenced by dememorize(), expon(), random_int(), and random_long().
| void lot::ran_array | ( | std::vector< long > & | aa, | |
| int | n | |||
| ) |
Updates long array of Knuth generator
| aa | the vector of values in which to update | |
| n | the number of values to update, note n == aa.size() assumed |
Definition at line 534 of file lot.cpp.
Referenced by ran_start().
| void lot::ranf_array | ( | std::vector< double > & | aa, | |
| int | n | |||
| ) |
Updates floating point array of Knuth generator
| aa | the vector of values in which to update | |
| n | the number of values to update, note n == aa.size() assumed |
Definition at line 410 of file lot.cpp.
Referenced by ranf_start().
| void lot::MT_sgenrand | ( | long | seed | ) |
Initializes Mersenne twister RNG
| seed |
http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.c
Here are the accompanying comments
A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome. http://www.math.keio.ac.jp/matumoto/emt.html email: matumoto@math.keio.ac.jp
Definition at line 725 of file lot.cpp.
Referenced by MT_genrand_int(), MT_init_by_array(), set_generator(), and set_seed().
| void lot::MT_init_by_array | ( | unsigned long * | init_key, | |
| int | key_length | |||
| ) |
Initialize Mersenne twister with an array
| init_key | is the array for initializing keys | |
| key_length | is its length |
Definition at line 745 of file lot.cpp.
References MT_sgenrand().
| void lot::MT_R_initialize | ( | int | seed | ) |
| unsigned long lot::MT_genrand_int | ( | void | ) |
generate random integer on [0,0xffffffff]-interval
Definition at line 800 of file lot.cpp.
References MT_sgenrand().
Referenced by MT_genrand(), MT_genrand_with_zero(), and MT_genrand_with_zero_one().
| double lot::MT_genrand | ( | void | ) |
generate random double uniform on (0,1)
Definition at line 839 of file lot.cpp.
References MT_genrand_int().
Referenced by set_generator(), and Set_MT().
| double lot::MT_genrand_with_zero | ( | void | ) |
generate random double uniform on [0,1)
Definition at line 846 of file lot.cpp.
References MT_genrand_int().
Referenced by Set_MT().
| double lot::MT_genrand_with_zero_one | ( | void | ) |
generate random double uniform on [0,1]
Definition at line 853 of file lot.cpp.
References MT_genrand_int().
Referenced by Set_MT().
| bool lot::Set_MT | ( | int | type | ) |
Set MT type
| type | DEFAULT (0,1), ZERO [0,1), ZERO_ONE [0,1] |
Definition at line 354 of file lot.cpp.
References MT_genrand(), MT_genrand_with_zero(), MT_genrand_with_zero_one(), OPEN, RAN_MT, ZERO, and ZERO_ONE.
Referenced by set_generator().
| double lot::beta | ( | double | aa, | |
| double | bb | |||
| ) |
Returns a random beta variate
| aa | The first beta parameter ( ) | |
| bb | The second beta parameter ( ) |
NOTE: Checks for aa, bb > 0 not included
NOTE: R uses RNGs uniform on [0,1). To ensure consistency with that well tested code make sure that you have Set_MT(ZERO), or the equivalent, if you are using the Mersenne-Twister. ZERO is the default.
This implementation is derived from R v1.9.0
Definition at line 938 of file lot.cpp.
References gamma(), t(), and unif_rand.
Referenced by proposeBeta().
| int lot::binom | ( | double | nin, | |
| double | pp | |||
| ) |
Returns a random binomial variate
| nin | sample size ( ) | |
| pp | probability of success on each trial ( ) |
This implementation is derived from R v1.9.0.
NOTE: Checks for finite nin and pp not included. Check for nin == floor(n+0.5) not included
Definition at line 1070 of file lot.cpp.
References f(), repeat, and unif_rand.
Referenced by multinom().
| double lot::cauchy | ( | double | l, | |
| double | s | |||
| ) |
Returns a Cauchy variate
| l | the location parameter | |
| s | the scale parameter |
Modified from R v2.0. Does not check isnan() on arguments. Does not check that arguments are finite
Definition at line 1252 of file lot.cpp.
References unif_rand.
| double lot::chisq | ( | double | n | ) |
| std::vector< double > lot::dirichlet | ( | std::vector< double > | c | ) |
Returns a vector of Dirichlet variates
| c | [vector<double>] parameters of the Dirichlet |
Definition at line 1310 of file lot.cpp.
References gamma().
Referenced by proposeDirch().
| double lot::expon | ( | void | ) |
Returns a random value from an exponential distribution with mean 1.
originally derived from R v1.8.1
NOTE: R uses RNGs uniform on [0,1). To ensure consistency with that well tested code make sure that you have Set_MT(ZERO), or the equivalent, if you are using the Mersenne-Twister. ZERO is the default.
Definition at line 1384 of file lot.cpp.
References uniform().
Referenced by exponential().
| double lot::exponential | ( | const double | lambda | ) | [inline] |
| double lot::f | ( | double | m, | |
| double | n | |||
| ) |
Returns an F variate
| m | ``numerator'' degrees of freedom | |
| n | ``denominator'' degrees of freedom |
Derived from R v2.0. Does not check arguments for isnan() or isfinite().
Definition at line 1480 of file lot.cpp.
References chisq().
Referenced by binom(), and hypergeom().
| double lot::gamma | ( | double | a, | |
| double | scale = 1.0 | |||
| ) |
Gamma random deviate
| a | Shape | |
| scale | Scale ( , defaults to 1.0) |
NOTE: Checks for finite scale and shape parameters not included
NOTE: R uses RNGs uniform on [0,1). To ensure consistency with that well tested code make sure that you have Set_MT(ZERO), or the equivalent, if you are using the Mersenne-Twister. ZERO is the default.
Derived from R v1.9.0
Definition at line 1551 of file lot.cpp.
References exp_rand, norm_rand, repeat, t(), and unif_rand.
Referenced by beta(), chisq(), dirichlet(), igamma(), and nbinom().
| double lot::geom | ( | double | p | ) |
| int lot::hypergeom | ( | int | nn1, | |
| int | nn2, | |||
| int | kk | |||
| ) |
Returns a hypergeometric variate
| nn1 | The number of white balls in the urn ( ) | |
| nn2 | The number of black balls in the urn ( ) | |
| kk | The sample size ( ) |
The code is modified from R v2.0 to take unsigned integer arguments rather than doubles. isfinite() checks are no longer needed. Check for n < r + b not done.
Definition at line 1812 of file lot.cpp.
References Density::dt(), f(), t(), and unif_rand.
| double lot::igamma | ( | const double | a, | |
| const double | s = 1.0 | |||
| ) | [inline] |
| double lot::lnorm | ( | double | logmean, | |
| double | logsd | |||
| ) |
Returns a log-normal deviate
| logmean | logarithm of the mean of the corresponding normal ( ) | |
| logsd | logarithm of the sd of the corresponding normal ( ) |
Derived from R v2.0. Does not do isnan() checks on arguments. Does not check for sigma > 0.
Definition at line 2093 of file lot.cpp.
References norm().
| double lot::logis | ( | double | location, | |
| double | scale | |||
| ) |
| std::vector< int > lot::multinom | ( | unsigned | n, | |
| const std::vector< double > & | p | |||
| ) |
| double lot::nbinom | ( | double | n, | |
| double | p | |||
| ) |
| int lot::poisson | ( | double | mu | ) |
Returns Poisson deviate
| mu | mean of the Poisson distribution ( ) |
derived from R v1.9.0
NOTE: Check for finite mu not included
NOTE: R uses RNGs uniform on [0,1). To ensure consistency with that well tested code make sure that you have Set_MT(ZERO), or the equivalent, if you are using the Mersenne-Twister. ZERO is the default.
Definition at line 2324 of file lot.cpp.
| double lot::norm | ( | const double | mu, | |
| const double | sd | |||
| ) | [inline] |
| double lot::snorm | ( | void | ) |
Returns standard normal deviate
Kinderman-Ramage standard normal generator from R v1.9.0
NOTE: R uses RNGs uniform on [0,1). To ensure consistency with that well tested code make sure that you have Set_MT(ZERO), or the equivalent, if you are using the Mersenne-Twister. ZERO is the default.
Definition at line 2506 of file lot.cpp.
References repeat, and unif_rand.
Referenced by norm().
| double lot::t | ( | double | df | ) |
Returns a t variate
| df | the degrees of freedom ( ) |
Derived from R v2.0. Does not check isnan() or isfinite() on argument.
Definition at line 2594 of file lot.cpp.
References chisq(), and norm_rand.
Referenced by beta(), gamma(), hypergeom(), poisson(), ran_start(), and ranf_start().
| double lot::weibull | ( | double | shape, | |
| double | scale | |||
| ) |
| double lot::get_ran_u_0 | ( | void | ) | const [inline] |
1.5.1