Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
RAND Class Reference

Random number generation class, uses various types of random generators depending on the implementation. More...

#include <Uniform.h>

+ Collaboration diagram for RAND:

Static Public Member Functions

static void init (unsigned long seed)
 Initialize the random generator's seed. More...
 
static void free ()
 Memory de-allocation. More...
 
static double Uniform ()
 Generates a random number from [0.0, 1.0[ uniformly distributed. More...
 
static unsigned int Uniform (unsigned int max)
 Returns a uniformly distributed random number from [0.0, max[. More...
 
static bool RandBool ()
 Returns a random boolean. More...
 
static unsigned long RandULong ()
 Return a random unsigned long, from uniform distribution. More...
 
static double gammln (double xx)
 From the Numerical Recieps. More...
 
static double Poisson (double mean)
 From the Numerical Recieps. More...
 
static double Gaussian (double sigma)
 
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 Bernoulli (double p)
 
static double Exponential (double mu)
 
static double Binomial (double p, unsigned int n)
 
static double Beta (const double a, const double b)
 
static unsigned int Binomial2 (double p, unsigned int n)
 
static void Multinomial (size_t K, unsigned int N, const double p[], unsigned int n[])
 
static void MultinomialOnNormalizedValarray (size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
 Multinomial draw assuming the probabilities sum to 1.0 and are all > 0. More...
 
static void MultinomialOnNormalizedValarray_expandedOut (size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
 Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N. More...
 
static void MultinomialOnNormalizedValarray_scrambleOut (size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
 Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N. More...
 
static void MultinomialOnNormalizedValarrayZipper_scrambleOut (size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
 Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N. More...
 
static void ScrambleArrayUInt (const int length, unsigned int *array)
 Randomize the elements within an array. More...
 
static void Sample (const int from, const int to, const unsigned int num, int *result, bool replace)
 Creates a sample of integers within range [from, to), with or without replacement. More...
 
static void SampleSeq (int from, int to, int by, unsigned int num, int *result, bool replace=false)
 
static void SampleSeqWithReciprocal (int from, int to, int by, unsigned int num1, int *result1, unsigned int num2, int *result2)
 
static size_t Discrete (const gsl_ran_discrete_t *g)
 Calling the GSL ran_discrete function. More...
 

Static Public Attributes

static long Seed1 = 0
 
static long Seed2 = 98280582
 

Private Member Functions

 RAND ()
 

Detailed Description

Random number generation class, uses various types of random generators depending on the implementation.

Constructor & Destructor Documentation

◆ RAND()

RAND::RAND ( )
private

Member Function Documentation

◆ Bernoulli()

static double RAND::Bernoulli ( double  p)
inlinestatic
430  {
431 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
432  return gsl_ran_bernoulli(r, p);
433 #else
434  double u = RAND::Uniform() ;
435 
436  if (u < p)
437  {
438  return 1 ;
439  }
440  else
441  {
442  return 0 ;
443  }
444 #endif
445  }
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:124

References Uniform().

Referenced by TTNeutralGenes::init_sequence(), and ParamsParser::rbernoul().

◆ Beta()

static double RAND::Beta ( const double  a,
const double  b 
)
inlinestatic
527  {
528  /*from Knuth*/
529  double x1 = RAND::Gamma(a, 1.0);
530  double x2 = RAND::Gamma(b, 1.0);
531 
532  return x1 / (x1 + x2);
533  }
static double Gamma(double a, double b)
Definition: Uniform.h:385

References Gamma().

Referenced by Binomial().

◆ Binomial()

static double RAND::Binomial ( double  p,
unsigned int  n 
)
inlinestatic
490  {
491  /*implements Knuth method*/
492  unsigned int i, a, b, k = 0;
493 
494  while (n > 10) /* This parameter is tunable */
495  {
496  double X;
497  a = 1 + (n / 2);
498  b = 1 + n - a;
499 
500  X = RAND::Beta((double) a, (double) b);
501 
502  if (X >= p)
503  {
504  n = a - 1;
505  p /= X;
506  }
507  else
508  {
509  k += a;
510  n = b - 1;
511  p = (p - X) / (1 - X);
512  }
513  }
514 
515  for (i = 0; i < n; i++)
516  {
517  double u = RAND::Uniform();
518  if (u < p)
519  k++;
520  }
521 
522  return k;
523 
524  }
static double Beta(const double a, const double b)
Definition: Uniform.h:526

References Beta(), and Uniform().

Referenced by Binomial2(), TProtoNeutralGenes::get_num_mutations(), Multinomial(), MultinomialOnNormalizedValarray(), MultinomialOnNormalizedValarray_expandedOut(), and MultinomialOnNormalizedValarray_scrambleOut().

◆ Binomial2()

static unsigned int RAND::Binomial2 ( double  p,
unsigned int  n 
)
inlinestatic
535  {
536 
537 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
538  return gsl_ran_binomial (r, p, n);
539 #else
540  return Binomial(p, n);
541 #endif
542  }
static double Binomial(double p, unsigned int n)
Definition: Uniform.h:489

References Binomial().

◆ BivariateGaussian()

static void RAND::BivariateGaussian ( double  sigma1,
double  sigma2,
double  rho,
double *  out1,
double *  out2 
)
inlinestatic
330  {
331 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
332  gsl_ran_bivariate_gaussian(r,sigma1,sigma2,rho,out1,out2);
333 #else
334  //gsl code:
335  double u, v, r2, scale;
336  //double *x = out, *y = (++out);
337  do
338  {
339  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
340 
341  u = -1 + 2 * Uniform ();
342  v = -1 + 2 * Uniform ();
343 
344  /* see if it is in the unit circle */
345  r2 = u * u + v * v;
346  }
347  while (r2 > 1.0 || r2 == 0);
348 
349  scale = sqrt (-2.0 * log (r2) / r2);
350 
351  *out1 = sigma1 * u * scale;
352  *out2 = sigma2 * (rho * u + sqrt(1 - rho*rho) * v) * scale;
353 
354 #endif
355  }

References Uniform().

Referenced by TProtoQuanti::getMutationEffectBivariateGaussian(), and TProtoQuanti::getMutationEffectBivariateGaussianLocSpec().

◆ Discrete()

static size_t RAND::Discrete ( const gsl_ran_discrete_t *  g)
inlinestatic

Calling the GSL ran_discrete function.

814  {
815 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
816  return gsl_ran_discrete(r, g);
817 #else
818  /* GSL code here, modified to call our Uniform() */
819  size_t c=0;
820  double u,f;
821  u = Uniform();
822 
823 #if KNUTH_CONVENTION
824  c = (u*(g->K));
825 #else
826  u *= g->K;
827  c = u;
828  u -= c;
829 #endif
830 
831  f = (g->F)[c];
832  /* fprintf(stderr,"c,f,u: %d %.4f %f\n",c,f,u); */
833  if (f == 1.0) return c;
834 
835  if (u < f) {
836  return c;
837  }
838  else {
839  return (g->A)[c];
840  }
841 
842 #endif
843  }

References Uniform().

Referenced by LCE_Breed_Selection::do_breed_selection_WrightFisher_1sex(), LCE_Breed_Selection::do_breed_selection_WrightFisher_2sex(), and LCE_Disperse_base::getMigrationIndexGSLdiscrete().

◆ Exponential()

static double RAND::Exponential ( double  mu)
inlinestatic
447  {
448  return -mu * log(RAND::Uniform());
449  }

References Uniform().

Referenced by ParamsParser::rexp(), and TProtoDeletMutations_bitstring::set_effects_exp().

◆ free()

static void RAND::free ( )
inlinestatic

Memory de-allocation.

109  {
110 
111 #if defined(HAS_SPRNG)
112  // do nothing with pointer?
113 #elif defined(HAS_GSL)
114  gsl_rng_free(r);
115 #endif
116 
117  }

Referenced by SimRunner::run().

◆ Gamma()

static double RAND::Gamma ( double  a,
double  b 
)
inlinestatic
385  {
386 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
387  return gsl_ran_gamma(r, a, b);
388 #else
389  /*pasting GSL code in here*/
390 
391  /* assume a > 0 */
392 
393  if (a < 1)
394  {
395  double u = Uniform();//might give singularity at 0....
396  return Gamma(1.0 + a, b) * pow (u, 1.0 / a);
397  }
398 
399  {
400  double x, v, u;
401  double d = a - 1.0 / 3.0;
402  double c = (1.0 / 3.0) / sqrt (d);
403 
404  while (1)
405  {
406  do
407  {
408  x = Gaussian(1.0); //should use the Ziggurat method?
409  v = 1.0 + c * x;
410  }
411  while (v <= 0);
412 
413  v = v * v * v;
414  u = Uniform();//might give singularity at 0....
415 
416  if (u < 1 - 0.0331 * x * x * x * x)
417  break;
418 
419  if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
420  break;
421  }
422 
423  return b * d * v;
424  }
425 
426 #endif
427  }
static double Gaussian(double sigma)
Definition: Uniform.h:261

References Gaussian(), and Uniform().

Referenced by Beta(), ParamsParser::rgamma(), and TProtoDeletMutations_bitstring::set_effects_gamma().

◆ gammln()

static double RAND::gammln ( double  xx)
inlinestatic

From the Numerical Recieps.

204  {
205  double x,y,tmp,ser=1.000000000190015;
206  static double cof[6]={76.18009172947146,-86.50532032941677,
207  24.01409824083091,-1.231739572450155,
208  0.1208650973866179e-2,-0.5395239384953e-5};
209  int j;
210  y=x=xx;
211  tmp=x+5.5;
212  tmp -= (x+0.5)*log(tmp);
213  for (j = 0; j < 6; ++j) ser += cof[j]/++y;
214 
215  return -tmp+log(2.5066282746310005*ser/x);
216 
217  }

Referenced by Poisson().

◆ Gaussian()

static double RAND::Gaussian ( double  sigma)
inlinestatic

From the GSL.

262  {
263 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
264 
265  return gsl_ran_gaussian_ziggurat (r, sigma);
266 
267 #else
269 // double x, y, r2;
270 //
271 // do
272 // {
273 // /* choose x,y in uniform square (-1,-1) to (+1,+1) */
274 //
275 // x = -1 + 2 * Uniform ( );
276 // y = -1 + 2 * Uniform ( );
277 //
278 // /* see if it is in the unit circle */
279 // r2 = x * x + y * y;
280 // }
281 // while (r2 > 1.0 || r2 == 0);
282 //
283 // /* Box-Muller transform */
284 // return sigma * y * sqrt (-2.0 * log (r2) / r2);
285 
286  /*trying the ratio method from GSL*/
287  /* see code in gsl/randist/gauss.c */
288  double u, v, x, y, Q;
289  const double s = 0.449871; /* Constants from Leva */
290  const double t = -0.386595;
291  const double a = 0.19600;
292  const double b = 0.25472;
293  const double r1 = 0.27597;
294  const double r2 = 0.27846;
295 
296  do /* This loop is executed 1.369 times on average */
297  {
298  /* Generate a point P = (u, v) uniform in a rectangle enclosing
299  the K+M region v^2 <= - 4 u^2 log(u). */
300 
301  /* u in (0, 1] to avoid singularity at u = 0 */
302  u = 1 - RAND::Uniform();
303 
304  /* v is in the asymmetric interval [-0.5, 0.5). However v = -0.5
305  is rejected in the last part of the while clause. The
306  resulting normal deviate is strictly symmetric about 0
307  (provided that v is symmetric once v = -0.5 is excluded). */
308  v = RAND::Uniform() - 0.5;
309 
310  /* Constant 1.7156 > sqrt(8/e) (for accuracy); but not by too
311  much (for efficiency). */
312  v *= 1.7156;
313 
314  /* Compute Leva's quadratic form Q */
315  x = u - s;
316  y = fabs (v) - t;
317  Q = x * x + y * (a * y - b * x);
318 
319  /* Accept P if Q < r1 (Leva) */
320  /* Reject P if Q > r2 (Leva) */
321  /* Accept if v^2 <= -4 u^2 log(u) (K+M) */
322  /* This final test is executed 0.012 times on average. */
323  }
324  while (Q >= r1 && (Q > r2 || v * v > -4 * u * u * log (u)));
325 
326  return sigma * (v / u);
327 #endif
328  }

References Uniform().

Referenced by Gamma(), LCE_PhenotypeExpression::get_env_cue_noise(), LCE_Selection_base::getFitnessMultivariateGaussian_VE(), LCE_Selection_base::getFitnessUnivariateGaussian_VE(), LCE_Breed_base::getGaussianFecundity(), TProtoQuanti::getMutationEffectUnivariateGaussian(), TProtoQuanti::getMutationEffectUnivariateGaussianLocSpec(), TTDispersal::init_sequence(), TTQuanti_continuous_full_pleio::init_sequence(), TTQuanti_continuous_var_pleio::init_sequence(), TTQuanti_continuous_no_pleio::init_sequence(), TTQuanti_continuous_full_pleio_epistasis::init_sequence(), TTQuanti_continuous_no_pleio_epistasis::init_sequence(), LCE_Patch_Extinction::rand_gaussian(), ParamsParser::rnorm(), and TProtoQuanti::set_trait_value_VE().

◆ init()

static void RAND::init ( unsigned long  seed)
inlinestatic

Initialize the random generator's seed.

82  {
83 
84 #if defined(HAS_SPRNG)
85  // initializing the SPRNG stream with default param and LFG (0) generator
86 
87  stream = SelectType(4); //MLFG Modified Lagged Fibonacci
88 
89  stream->init_sprng( _myenv->workerRank(), _myenv->workerCount()+1, seed, SPRNG_DEFAULT );
90 
91 // message("--- initialized the SPRNG random generator on rank %i\n", _myenv->workerRank());
92 //
93 // stream->print_sprng();
94 //
95 // message("--- process %i random number: %.14f\n", _myenv->workerRank(), stream->sprng());
96 
97 #elif defined(HAS_GSL)
98 
99  init_gsl( gsl_rng_mt19937, seed );
100 
101 #else
102 
103  Seed1 = seed;
104 
105 #endif
106  }
MPIenv * _myenv
Definition: MPImanager.cc:36
int workerCount() const
Definition: MPImanager.h:129
int workerRank() const
Definition: MPImanager.h:130
static long Seed1
Definition: Uniform.h:78

References _myenv, Seed1, MPIenv::workerCount(), and MPIenv::workerRank().

Referenced by SimRunner::init_random_seed(), and SimRunner::run().

◆ LogNormal()

static double RAND::LogNormal ( double  zeta,
double  sigma 
)
inlinestatic
357  {
358 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
359  return gsl_ran_lognormal(r,zeta,sigma);
360 #else
361  //this is the GSL code:
362  double u, v, r2, normal, z;
363 
364  do
365  {
366  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
367 
368  u = -1 + 2 * Uniform();
369  v = -1 + 2 * Uniform();
370 
371  /* see if it is in the unit circle */
372  r2 = u * u + v * v;
373  }
374  while (r2 > 1.0 || r2 == 0);
375 
376  normal = u * sqrt (-2.0 * log (r2) / r2);
377 
378  z = exp (sigma * normal + zeta);
379 
380  return z;
381 
382 #endif
383  }

References Uniform().

Referenced by LCE_Breed_base::getLogNormalFecundity(), LCE_Patch_Extinction::rand_lognormal(), ParamsParser::rlognorm(), and TProtoDeletMutations_bitstring::set_effects_lognorm().

◆ Multinomial()

static void RAND::Multinomial ( size_t  K,
unsigned int  N,
const double  p[],
unsigned int  n[] 
)
inlinestatic
545  {
546 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
547  return gsl_ran_multinomial (r, K, N, p, n);
548 #else
549  // GSL code here:
550  size_t k;
551  double norm = 0.0;
552  double sum_p = 0.0;
553 
554  unsigned int sum_n = 0;
555 
556  /* p[k] may contain non-negative weights that do not sum to 1.0.
557  * Even a probability distribution will not exactly sum to 1.0
558  * due to rounding errors.
559  */
560 
561  for (k = 0; k < K; k++)
562  {
563  norm += p[k];
564  }
565 
566  for (k = 0; k < K; k++)
567  {
568  if (p[k] > 0.0)
569  {
570  n[k] = Binomial (p[k] / (norm - sum_p), N - sum_n); //MOD
571  }
572  else
573  {
574  n[k] = 0;
575  }
576 
577  sum_p += p[k];
578  sum_n += n[k];
579  }
580 
581 #endif
582  }

References Binomial().

◆ MultinomialOnNormalizedValarray()

static void RAND::MultinomialOnNormalizedValarray ( size_t  K,
unsigned int  N,
const std::valarray< double > &  p,
unsigned int  n[] 
)
inlinestatic

Multinomial draw assuming the probabilities sum to 1.0 and are all > 0.

586  {
587  // GSL code here: FG 2020: modified for normalized arrays
588  size_t k;
589  double sum_p = 0.0;
590 
591  unsigned int sum_n = 0;
592 
593  for (k = 0; k < K; k++)
594  {
595  n[k] = RAND::Binomial (p[k] / (1.0 - sum_p), N - sum_n); //MOD FG
596  sum_p += p[k];
597  sum_n += n[k];
598  }
599  }

References Binomial().

◆ MultinomialOnNormalizedValarray_expandedOut()

static void RAND::MultinomialOnNormalizedValarray_expandedOut ( size_t  K,
unsigned int  N,
const std::valarray< double > &  p,
unsigned int  n[] 
)
inlinestatic

Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N.

605  {
606  // GSL code here: FG 2020: modified for normalized arrays
607  size_t k;
608  double sum_p = 0.0;
609  unsigned int _n, sum_n = 0;
610  unsigned int i = 0, pos = 0;
611 
612  for (k = 0; k < K; k++) {
613  //draw number of events k with probability p[k]
614  _n = RAND::Binomial (p[k] / (1.0 - sum_p), N - sum_n);
615 
616  for(i = 0; i < _n; i++)
617  n[pos++] = k;
618 
619  sum_p += p[k];
620  sum_n += _n;
621  assert(pos <= N);
622  }
623  assert(sum_n == N);
624  }

References Binomial().

◆ MultinomialOnNormalizedValarray_scrambleOut()

static void RAND::MultinomialOnNormalizedValarray_scrambleOut ( size_t  K,
unsigned int  N,
const std::valarray< double > &  p,
unsigned int  n[] 
)
inlinestatic

Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N.

630  {
631  // GSL code here: FG 2020: modified for normalized arrays
632  size_t k;
633  double sum_p = 0.0;
634  unsigned int _n, sum_n = 0;
635  unsigned int i = 0, pos = 0;
636 
637  for (k = 0; k < K; k++) {
638  //draw number of events k with probability p[k]
639  _n = RAND::Binomial (p[k] / (1.0 - sum_p), N - sum_n);
640 
641  for(i = 0; i < _n; i++)
642  n[pos++] = k;
643 
644  sum_p += p[k];
645  sum_n += _n;
646  assert(pos <= N);
647  }
648  assert(sum_n == N);
649 
650  //scramble the output array
652 
653  }
static void ScrambleArrayUInt(const int length, unsigned int *array)
Randomize the elements within an array.
Definition: Uniform.h:689

References Binomial(), and ScrambleArrayUInt().

◆ MultinomialOnNormalizedValarrayZipper_scrambleOut()

static void RAND::MultinomialOnNormalizedValarrayZipper_scrambleOut ( size_t  K,
unsigned int  N,
const std::valarray< double > &  p,
unsigned int  n[] 
)
inlinestatic

Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of size N.

659  {
660  size_t k;
661  double sum = 0.0;
662  double rand;
663  unsigned int i = 0;
664 
665  for (k = 0; k < N; k++) {
666 
667  rand = std::min(0.99999995, RAND::Uniform()); // to avoid problems with rounding errors
668  i = 0;
669  sum = p[i];
670 
671  //zip through the proba array:
672  while( sum < rand ) {
673  i++;
674  sum += p[i];
675  }
676 
677  n[k] = i;
678 
679  }
680  //scramble the output array
682 
683  }

References ScrambleArrayUInt(), and Uniform().

◆ Poisson()

static double RAND::Poisson ( double  mean)
inlinestatic

From the Numerical Recieps.

219  {
220 
221 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
222  return gsl_ran_poisson(r, mean);
223 #else
224  static double sq,alxm,g,oldm=(-1.0);
225  double em,t,y;
226 
227  if (mean < 12.0)
228  {
229  if (mean != oldm){
230  oldm=mean;
231  g=exp(-mean);
232  }
233  em = -1;
234  t=1.0;
235  do {
236  ++em;
237  t *= Uniform();
238  } while (t > g);
239  }else
240  {
241  if (mean != oldm)
242  {
243  oldm=mean;
244  sq=sqrt(2.0*mean);
245  alxm=log(mean);
246  g=mean*alxm-gammln(mean+1.0);
247  }
248  do {
249  do {
250  y=tan(M_PI*Uniform());
251  em=sq*y+mean;
252  } while (em < 0.0);
253  em=floor(em);
254  t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
255  } while (Uniform() > t);
256  }
257  return em;
258 #endif
259  }
static double gammln(double xx)
From the Numerical Recieps.
Definition: Uniform.h:204

References gammln(), and Uniform().

Referenced by LCE_Breed_base::getPoissonFecundity(), TTDispersal::mutate(), TProtoQuanti::mutate_diallelic_no_pleio(), TProtoQuanti::mutate_diallelic_pleio(), TProtoQuanti::mutate_diallelic_var_pleio(), TT_BDMI::mutate_diplo(), TProtoQuanti::mutate_full_pleio(), TT_BDMI::mutate_haplo(), TProtoQuanti::mutate_inplace_full_pleio(), TProtoQuanti::mutate_inplace_no_pleio(), TProtoQuanti::mutate_inplace_var_pleio(), TProtoQuanti::mutate_no_pleio(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), TProtoQuanti::mutate_var_pleio(), LCE_Patch_Extinction::rand_poisson(), GeneticMap::recombine(), ParamsParser::rpoiss(), and LCE_Breed_Disperse::stochasticLogisticGrowth().

◆ RandBool()

static bool RAND::RandBool ( )
inlinestatic

Returns a random boolean.

162  {
163  //generate a random int (or long)
164 #ifdef HAS_SPRNG
165  static int intrand = stream->isprng();
166 #elif defined(HAS_GSL)
167  static unsigned long int intrand = gsl_rng_get(r);
168 #else
169  static int intrand = (int)(Uniform()*(double)std::numeric_limits<int>::max());
170 #endif
171  //read up to the first 16 bits
172  static unsigned int num = 0;
173  //redraw an number after that limit
174  if ( ++num > 16 ) {
175  num = 0;
176 
177 #ifdef HAS_SPRNG
178  intrand = stream->isprng();
179 #elif defined(HAS_GSL)
180  intrand = gsl_rng_get(r);
181 #else
182  intrand = (int)(Uniform()*(double)std::numeric_limits<int>::max());
183 #endif
184 
185  }
186  return (intrand & (1 << num) );
187 
188  }

References Uniform().

Referenced by LCE_Patch_Extinction::do_remove(), TProtoQuanti::getMutationEffectBivariateDiallelic(), TProtoQuanti::getMutationEffectUnivariateDiallelic(), LCE_Breed_base::getOffsprgSexFixed(), LCE_Breed_base::getOffsprgSexRandom(), TTDispersal::inherit(), TProtoBDMI::inherit_free(), TProtoDeletMutations_bitstring::inherit_free(), TProtoQuanti::inherit_free(), TProtoNeutralGenes::inherit_free(), TTQuanti_continuous_full_pleio::init_sequence(), TTQuanti_continuous_var_pleio::init_sequence(), TTQuanti_continuous_no_pleio::init_sequence(), TTQuanti_diallelic_no_pleio::init_sequence(), TTQuanti_diallelic_full_pleio::init_sequence(), TTQuanti_diallelic_var_pleio::init_sequence(), TTQuanti_diallelic_bitstring_no_pleio::init_sequence(), TTQuanti_diallelic_bitstring_full_pleio::init_sequence(), TTQuanti_diallelic_bitstring_var_pleio::init_sequence(), TTQuanti_continuous_full_pleio_epistasis::init_sequence(), TTQuanti_diallelic_full_pleio_epistasis::init_sequence(), TTQuanti_continuous_no_pleio_epistasis::init_sequence(), TTQuanti_diallelic_no_pleio_epistasis::init_sequence(), LCE_Disperse_EvolDisp::Migrate_SteppingStone1D(), TTDispersal::mutate(), TTNeutralGenes::mutate_2all(), TProtoQuanti::mutate_diallelic_no_pleio(), TProtoQuanti::mutate_diallelic_pleio(), TProtoQuanti::mutate_diallelic_var_pleio(), TT_BDMI::mutate_diplo(), TProtoQuanti::mutate_full_pleio(), TProtoQuanti::mutate_inplace_full_pleio(), TProtoQuanti::mutate_inplace_no_pleio(), TProtoQuanti::mutate_inplace_var_pleio(), TTNeutralGenes::mutate_KAM(), TProtoQuanti::mutate_no_pleio(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes::mutate_SSM(), TProtoQuanti::mutate_var_pleio(), GeneticMap::recombine(), LCE_Breed_Wolbachia::wolbachia_model_1(), and LCE_Breed_Wolbachia::wolbachia_model_2().

◆ RandULong()

static unsigned long RAND::RandULong ( )
inlinestatic

Return a random unsigned long, from uniform distribution.

191  {
192 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
193  return gsl_rng_get(r);
194 #else
195  unsigned long rnd, limit = 0x10000000; //=2^28 this gives ~7% redraws
196  do{
197  rnd = (unsigned long)(Uniform()*ULONG_MAX);
198  }while(rnd < limit);
199 
200  return rnd;
201 #endif
202  }

References Uniform().

◆ Sample()

static void RAND::Sample ( const int  from,
const int  to,
const unsigned int  num,
int *  result,
bool  replace 
)
inlinestatic

Creates a sample of integers within range [from, to), with or without replacement.

Can be used to scramble an array (without replacement). The random sequence of integers is placed in the 'results' array.

Parameters
fromstarting number of the series
tolast number of the series
numnumber of elements to draw within [from, to)
resultcontainer to hold the resulting randomized sequence of integers
714  {
715  assert(from < to);
716 
717  unsigned int seq_length = to - from; //don't include last (to)
718 
719  assert(num <= seq_length);
720 
721  // we build a sequence of indexes to choose from
722  int *seq = new int [seq_length];
723 
724  seq[0] = from;
725 
726  for(unsigned int i = 1; seq[i-1] < to && i < seq_length; ++i)
727  seq[i] = seq[i-1] + 1;
728 
729  if(!replace) { //without replacement
730 
731  unsigned int size = seq_length, pos, last = seq_length - 1;
732 
733  for (unsigned int i = 0; i < num; i++) {
734  pos = RAND::Uniform(size);
735  result[i] = seq[pos];
736  seq[pos] = seq[last];
737  // seq[last] = result[i]; useless operation
738  size--; last--;
739  }
740 
741  } else { //with replacement
742  for (unsigned int i = 0; i < num; i++)
743  result[i] = seq[ RAND::Uniform(seq_length) ];
744  }
745 
746  delete [] seq;
747  }

References Uniform().

Referenced by MPFileHandler::createAndPrintSample(), and FileServices::subSamplePatch().

◆ SampleSeq()

static void RAND::SampleSeq ( int  from,
int  to,
int  by,
unsigned int  num,
int *  result,
bool  replace = false 
)
inlinestatic
750  {
751  assert(from < to && by < to && by > 0);
752 
753  unsigned int seq_length = (int)((to - from) / by); //don't include last (to)
754 
755  assert(num <= seq_length);
756 
757  int *seq = new int [seq_length];
758 
759  seq[0] = from;
760 
761  for(unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
762 
763  if(!replace) { //without replacement
764 
765  unsigned int size = seq_length, pos, last = seq_length - 1;
766 
767  for (unsigned int i = 0; i < num; i++) {
768  pos = RAND::Uniform(size);
769  result[i] = seq[pos];
770  seq[pos] = seq[last];
771  // seq[last] = result[i]; useless operation
772  size--; last--;
773  }
774 
775  } else { //with replacement
776  for (unsigned int i = 0; i < num; i++) result[i] = seq[ RAND::Uniform(seq_length) ];
777  }
778 
779  delete [] seq;
780  }

References Uniform().

◆ SampleSeqWithReciprocal()

static void RAND::SampleSeqWithReciprocal ( int  from,
int  to,
int  by,
unsigned int  num1,
int *  result1,
unsigned int  num2,
int *  result2 
)
inlinestatic
783  {
784  assert(from < to && by < to && by > 0);
785 
786  unsigned int seq_length = (int)((to - from) / by); //don't include last (to)
787 
788  assert(num1 + num2 == seq_length);
789 
790  int *seq = new int [seq_length];
791 
792  seq[0] = from;
793 
794  for(unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
795 
796  unsigned int size = seq_length, pos, last = seq_length - 1;
797 
798  for (unsigned int i = 0; i < num1; i++) {
799  pos = RAND::Uniform(size);
800  result1[i] = seq[pos];
801  seq[pos] = seq[last];
802  seq[last] = result1[i];
803  size--; last--;
804  }
805 
806  for (unsigned int i = 0; i < num2 && i < size; i++)
807  result2[i] = seq[i];
808 
809  delete [] seq;
810  }

References Uniform().

Referenced by LCE_NtrlInit::init_allele_freq(), and LCE_QuantiInit::init_allele_freq().

◆ ScrambleArrayUInt()

static void RAND::ScrambleArrayUInt ( const int  length,
unsigned int *  array 
)
inlinestatic

Randomize the elements within an array.

Parameters
lengththe length of the array
arraythe array to scramble
690  {
691  unsigned int size = length, pos, last = length - 1, num = length -1, el;
692 
693  // with stop after 2 elements are left
694  // this algo allows swapping an element with itself, which is fine
695  for (unsigned int i = 0; i < num; i++) {
696  pos = RAND::Uniform(size);
697  el = array[last];
698  array[last] = array[pos];
699  array[pos] = el;
700  size--; last--;
701  }
702 
703  assert(size == 1); //we stopped before swapping the last (first) element with itself
704  }

References Uniform().

Referenced by MultinomialOnNormalizedValarray_scrambleOut(), and MultinomialOnNormalizedValarrayZipper_scrambleOut().

◆ Uniform() [1/2]

static double RAND::Uniform ( )
inlinestatic

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.

124  {
125 
126 #ifdef HAS_SPRNG
127  return stream->sprng();
128 #elif defined(HAS_GSL)
129  return gsl_rng_uniform(r);
130 #else
131  register long z, w;
132 
133  do{
134  w = Seed1 / 53668;
135 
136  Seed1 = 40014 * (Seed1 - w * 53668) - w * 12211;
137 
138  if (Seed1 < 0) Seed1 += 2147483563;
139 
140  w = (Seed2 / 52774);
141 
142  Seed2 = 40692 * (Seed2 - w * 52774) - w * 3791;
143 
144  if (Seed2 < 0) Seed2 += 2147483399;
145 
146  z = Seed1 - Seed2;
147 
148  if (z < 1) z += 2147483562;
149 
150  }while (!((z * 4.656613e-10) < 1.0));
151 
152  return (z * 4.656613e-10);
153 #endif
154  }
static long Seed2
Definition: Uniform.h:78

References Seed1, and Seed2.

Referenced by Bernoulli(), Binomial(), BivariateGaussian(), LCE_Breed_base::checkPolygyny(), LCE_Cross::create_individual_ancestors(), Discrete(), LCE_Patch_Extinction::do_remove(), LCE_Selection_base::doViabilitySelection(), LCE_Disperse_EvolDisp::evoldisp(), LCE_BreedAssortativeMating::execute(), LCE_Aging::execute(), LCE_Patch_Extinction::execute(), Exponential(), BinaryDataLoader::extractPop(), Metapop::fillPopulationFromSource(), LCE_Disperse_EvolDisp::fixdisp(), LCE_Breed_base::fullPolyginy_manyMales(), Gamma(), Gaussian(), LCE_Breed_Disperse::get_parent(), LCE_Disperse_base::getMigrationIndex(), LCE_Disperse_base::getMigrationPatchBackward(), LCE_Disperse_base::getMigrationPatchForward(), TProtoQuanti::getMutationEffectBivariateDiallelic(), LCE_QuantiInit::init_allele_freq(), TT_BDMI::init_sequence(), TTDeletMutations_bitstring::init_sequence(), TTDispersal::init_sequence(), TTNeutralGenes::init_sequence(), LogNormal(), LCE_Breed_Selection::makeOffspringWithSelection(), LCE_Breed_Disperse::mate_selfing(), LCE_Disperse_EvolDisp::Migrate_Island(), LCE_Disperse_EvolDisp::Migrate_Island_Propagule(), LCE_Disperse_EvolDisp::Migrate_Lattice(), LCE_Disperse_ConstDisp::MigratePatchByNumber(), MultinomialOnNormalizedValarrayZipper_scrambleOut(), TTDispersal::mutate(), TTWolbachia::mutate(), TTNeutralGenes::mutate_2all(), TProtoQuanti::mutate_diallelic_no_pleio(), TProtoQuanti::mutate_diallelic_pleio(), TProtoQuanti::mutate_diallelic_var_pleio(), TT_BDMI::mutate_diplo(), TProtoQuanti::mutate_full_pleio(), TT_BDMI::mutate_haplo(), TProtoQuanti::mutate_inplace_full_pleio(), TProtoQuanti::mutate_inplace_no_pleio(), TProtoQuanti::mutate_inplace_var_pleio(), TTNeutralGenes::mutate_KAM(), TProtoQuanti::mutate_no_pleio(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes::mutate_SSM(), TProtoQuanti::mutate_var_pleio(), 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(), GeneticMap::recombine(), LCE_Resize::regulateAgeClassNoBackup(), LCE_Resize::regulateAgeClassWithBackup(), LCE_Regulation::regulatePatch(), ParamsParser::runif(), Sample(), LCE_Cross::sampleAmongPop(), SampleSeq(), SampleSeqWithReciprocal(), LCE_Cross::sampleWithinPop(), ScrambleArrayUInt(), LCE_BreedAssortativeMating::ScrambleContainer(), LCE_Disperse_base::setPropaguleTargets(), TTProtoWithMap::setRecombinationMapRandom(), setSpatialMatrix(), LCE_Breed_Disperse::stochasticFecundityGrowth(), Uniform(), LCE_Breed_Wolbachia::wolbachia_model_1(), LCE_Breed_base::WrightFisherPopulation(), and LCE_Breed_Quanti::WrightFisherPopulation().

◆ Uniform() [2/2]

static unsigned int RAND::Uniform ( unsigned int  max)
inlinestatic

Returns a uniformly distributed random number from [0.0, max[.

157  {
158  return (unsigned int)(Uniform() * max);
159  }

References Uniform().

Member Data Documentation

◆ Seed1

long RAND::Seed1 = 0
static

Referenced by init(), and Uniform().

◆ Seed2

long RAND::Seed2 = 98280582
static

Referenced by Uniform().


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR