|
Nemo
2.2.0
|
Random number generation class, uses various types of random generators depending on the implementation. More...
#include <Uniform.h>
Static Public Member Functions | |
| static void | init (unsigned long seed) |
| Initialize the random generator's seed. | |
| static double | Uniform () |
| Generates a random number from [0.0, 1.0[ uniformly distributed. | |
| static unsigned int | Uniform (unsigned int max) |
| Returns a uniformly distributed random number from [0.0, max[. | |
| static bool | RandBool () |
| Returns a random boolean. | |
| static unsigned long | RandULong () |
| Return a random unsigned long, from uniform distribution. | |
| static double | gammln (double xx) |
| From the Numerical Recieps. | |
| static double | Poisson (double mean) |
| From the Numerical Recieps. | |
| static double | Gaussian (double sigma) |
| From the GSL. | |
| static void | BivariateGaussian (double sigma1, double sigma2, double rho, double *out1, double *out2) |
| static double | LogNormal (double zeta, double sigma) |
| static double | Gamma (double a, double b) |
| static double | Exponential (double mu) |
Public Attributes | |
| static long | Seed2 = 98280582 |
Static Public Attributes | |
| static long | Seed1 = 0 |
Private Member Functions | |
| RAND () | |
Random number generation class, uses various types of random generators depending on the implementation.
| RAND::RAND | ( | ) | [private] |
| static void RAND::BivariateGaussian | ( | double | sigma1, |
| double | sigma2, | ||
| double | rho, | ||
| double * | out1, | ||
| double * | out2 | ||
| ) | [inline, static] |
Definition at line 253 of file Uniform.h.
References Uniform().
{
#ifdef HAS_GSL
gsl_ran_bivariate_gaussian(r,sigma1,sigma2,rho,out1,out2);
#else
//gsl code:
double u, v, r2, scale;
//double *x = out, *y = (++out);
do
{
/* choose x,y in uniform square (-1,-1) to (+1,+1) */
u = -1 + 2 * Uniform ();
v = -1 + 2 * Uniform ();
/* see if it is in the unit circle */
r2 = u * u + v * v;
}
while (r2 > 1.0 || r2 == 0);
scale = sqrt (-2.0 * log (r2) / r2);
*out1 = sigma1 * u * scale;
*out2 = sigma2 * (rho * u + sqrt(1 - rho*rho) * v) * scale;
#endif
}
| static double RAND::Exponential | ( | double | mu | ) | [inline, static] |
Definition at line 316 of file Uniform.h.
References Uniform().
Referenced by TProtoDeletMutations_bitstring::set_effects_exp().
{
return -mu * log(RAND::Uniform());
}
| static double RAND::Gamma | ( | double | a, |
| double | b | ||
| ) | [inline, static] |
Definition at line 308 of file Uniform.h.
References fatal().
Referenced by TProtoDeletMutations_bitstring::set_effects_gamma().
{
#ifdef HAS_GSL
return gsl_ran_gamma(r, a, b);
#else
fatal("RAND::Gamma unimplemented; please link with the GNU Scientific Library to get it.");
#endif
}
| static double RAND::gammln | ( | double | xx | ) | [inline, static] |
From the Numerical Recieps.
Definition at line 174 of file Uniform.h.
Referenced by Poisson().
{
double x,y,tmp,ser=1.000000000190015;
static double cof[6]={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*log(tmp);
for (j = 0; j < 6; ++j) ser += cof[j]/++y;
return -tmp+log(2.5066282746310005*ser/x);
}
| static double RAND::Gaussian | ( | double | sigma | ) | [inline, static] |
From the GSL.
Definition at line 227 of file Uniform.h.
References Uniform().
Referenced by LCE_Selection_base::getFitnessMultivariateGaussian_VE(), LCE_Selection_base::getFitnessUnivariateGaussian_VE(), LCE_Breed_base::getGaussianFecundity(), and LCE_Patch_Extinction::rand_gaussian().
{
#ifdef HAS_GSL
return gsl_ran_gaussian (r, sigma);
#else
double x, y, r2;
do
{
/* choose x,y in uniform square (-1,-1) to (+1,+1) */
x = -1 + 2 * Uniform ( );
y = -1 + 2 * Uniform ( );
/* see if it is in the unit circle */
r2 = x * x + y * y;
}
while (r2 > 1.0 || r2 == 0);
/* Box-Muller transform */
return sigma * y * sqrt (-2.0 * log (r2) / r2);
#endif
}
| static void RAND::init | ( | unsigned long | seed | ) | [inline, static] |
| static double RAND::LogNormal | ( | double | zeta, |
| double | sigma | ||
| ) | [inline, static] |
Definition at line 280 of file Uniform.h.
References Uniform().
Referenced by LCE_Patch_Extinction::rand_lognormal(), and TProtoDeletMutations_bitstring::set_effects_lognorm().
{
#ifdef HAS_GSL
return gsl_ran_lognormal(r,zeta,sigma);
#else
//this is the GSL code:
double u, v, r2, normal, z;
do
{
/* choose x,y in uniform square (-1,-1) to (+1,+1) */
u = -1 + 2 * Uniform();
v = -1 + 2 * Uniform();
/* see if it is in the unit circle */
r2 = u * u + v * v;
}
while (r2 > 1.0 || r2 == 0);
normal = u * sqrt (-2.0 * log (r2) / r2);
z = exp (sigma * normal + zeta);
return z;
#endif
}
| static double RAND::Poisson | ( | double | mean | ) | [inline, static] |
From the Numerical Recieps.
Definition at line 189 of file Uniform.h.
References gammln(), and Uniform().
Referenced by LCE_Breed_base::getPoissonFecundity(), TTNeutralGenes::inherit_low(), TTNeutralGenes::mutate_KAM(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes::mutate_SSM(), LCE_Patch_Extinction::rand_poisson(), and TTDeletMutations_bitstring::recombine().
{
static double sq,alxm,g,oldm=(-1.0);
double em,t,y;
if (mean < 12.0)
{
if (mean != oldm){
oldm=mean;
g=exp(-mean);
}
em = -1;
t=1.0;
do {
++em;
t *= Uniform();
} while (t > g);
}else
{
if (mean != oldm)
{
oldm=mean;
sq=sqrt(2.0*mean);
alxm=log(mean);
g=mean*alxm-gammln(mean+1.0);
}
do {
do {
y=tan(M_PI*Uniform());
em=sq*y+mean;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
} while (Uniform() > t);
}
return em;
}
| static bool RAND::RandBool | ( | ) | [inline, static] |
Returns a random boolean.
Definition at line 133 of file Uniform.h.
References Uniform().
Referenced by LCE_Breed_base::getOffsprgSexFixed(), LCE_Breed_base::getOffsprgSexRandom(), TTDeletMutations_bitstring::inherit_0(), TTNeutralGenes::inherit_free(), TTDeletMutations_bitstring::inherit_free(), TTNeutralGenes::inherit_low(), LCE_Disperse_EvolDisp::Migrate_SteppingStone1D(), TTDeletMutations_bitstring::mutate_noredraw(), TTNeutralGenes::mutate_SSM(), TTDeletMutations_bitstring::recombine(), LCE_Breed_Wolbachia::wolbachia_model_1(), and LCE_Breed_Wolbachia::wolbachia_model_2().
{
//generate a random int (or long)
#ifdef HAS_SPRNG
static int intrand = isprng();
#elif defined(HAS_GSL)
static unsigned long int intrand = gsl_rng_get(r);
#else
static int intrand = (int)(Uniform()*(double)std::numeric_limits<int>::max());
#endif
//read up to the first 16 bits
static unsigned int num = 0;
//redraw an number after that limit
if ( ++num > 16 ) {
num = 0;
#ifdef HAS_SPRNG
intrand = isprng();
#elif defined(HAS_GSL)
intrand = gsl_rng_get(r);
#else
intrand = (int)(Uniform()*(double)std::numeric_limits<int>::max());
#endif
}
return (intrand & (1 << num) );
}
| static unsigned long RAND::RandULong | ( | ) | [inline, static] |
Return a random unsigned long, from uniform distribution.
Definition at line 161 of file Uniform.h.
References Uniform().
{
#ifdef HAS_GSL
return gsl_rng_get(r);
#else
unsigned long rnd, limit = 0x10000000; //=2^28 this gives ~7% redraws
do{
rnd = (unsigned long)(Uniform()*ULONG_MAX);
}while(rnd < limit);
return rnd;
#endif
}
| static double RAND::Uniform | ( | ) | [inline, static] |
Generates a random number from [0.0, 1.0[ uniformly distributed.
If SPRNG or GSL libraries are not used, implement a random generator from: L'Ecuyer, 1988, "Efficient and Portable Combined Random Number Generators", Communication of the ACM, 31(6):742-774.
Definition at line 98 of file Uniform.h.
Referenced by BivariateGaussian(), LCE_Breed_base::checkPolygyny(), LCE_Selection_base::doViabilitySelection(), LCE_Disperse_EvolDisp::evoldisp(), Exponential(), Metapop::fillPopulationFromSource(), LCE_Disperse_EvolDisp::fixdisp(), LCE_Breed_base::fullPolyginy_manyMales(), Gaussian(), LCE_Breed_Disperse::get_parent(), LCE_Disperse_base::getMigrationPatchBackward(), LCE_Disperse_base::getMigrationPatchForward(), TTNeutralGenes::inherit_low(), TTNeutralGenes::init_sequence(), TTDeletMutations_bitstring::init_sequence(), LogNormal(), LCE_Breed_Selection::makeOffspringWithSelection(), LCE_Breed_Disperse::mate_selfing(), LCE_Disperse_EvolDisp::Migrate_Island(), LCE_Disperse_EvolDisp::Migrate_Island_Propagule(), TTWolbachia::mutate(), TTNeutralGenes::mutate_KAM(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes::mutate_SSM(), LCE_Breed_base::partialMonoginy(), LCE_Breed_base::partialPolyginy(), LCE_Breed_base::partialPolyginy_manyMales(), LCE_Breed_base::partialSelfing(), Poisson(), LCE_Patch_Extinction::rand_exp(), LCE_Patch_Extinction::rand_uniform(), RandBool(), LCE_Breed_base::random_hermaphrodite(), LCE_Breed_base::RandomMating(), RandULong(), TTDeletMutations_bitstring::recombine(), LCE_Resize::regulateAgeClassNoBackup(), LCE_Resize::regulateAgeClassWithBackup(), LCE_Disperse_base::setPropaguleTargets(), LCE_Selection_base::setSpatialMatrix(), and LCE_Breed_Wolbachia::wolbachia_model_1().
{
#ifdef HAS_SPRNG
return sprng();
#elif defined(HAS_GSL)
return gsl_rng_uniform(r);
#else
register long z, w;
do{
w = Seed1 / 53668;
Seed1 = 40014 * (Seed1 - w * 53668) - w * 12211;
if (Seed1 < 0) Seed1 += 2147483563;
w = (Seed2 / 52774);
Seed2 = 40692 * (Seed2 - w * 52774) - w * 3791;
if (Seed2 < 0) Seed2 += 2147483399;
z = Seed1 - Seed2;
if (z < 1) z += 2147483562;
}while (!((z * 4.656613e-10) < 1.0));
return (z * 4.656613e-10);
#endif
}
| static unsigned int RAND::Uniform | ( | unsigned int | max | ) | [inline, static] |
long RAND::Seed1 = 0 [static] |
| long RAND::Seed2 = 98280582 |
1.7.5.1 -- Nemo is hosted by