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
433  {
434 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
435  return gsl_ran_bernoulli(r, p);
436 #else
437  double u = RAND::Uniform() ;
438 
439  if (u < p)
440  {
441  return 1 ;
442  }
443  else
444  {
445  return 0 ;
446  }
447 #endif
448  }
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:127

References Uniform().

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

◆ Beta()

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

References Gamma().

Referenced by Binomial().

◆ Binomial()

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

References Beta(), and Uniform().

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

◆ Binomial2()

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

References Binomial().

◆ BivariateGaussian()

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

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.

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

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
450  {
451  return -mu * log(RAND::Uniform());
452  }

References Uniform().

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

◆ free()

static void RAND::free ( )
inlinestatic

Memory de-allocation.

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

Referenced by SimRunner::run().

◆ Gamma()

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

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.

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

Referenced by Poisson().

◆ Gaussian()

static double RAND::Gaussian ( double  sigma)
inlinestatic

From the GSL.

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

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 
92 #ifdef DEBUG_MPI
93  message("--- initialized the SPRNG random generator on rank %i\n", _myenv->workerRank());
94 
95  stream->print_sprng();
96 
97  message("--- process %i random number: %.14f\n", _myenv->workerRank(), stream->sprng());
98 #endif
99 
100 #elif defined(HAS_GSL)
101 
102  init_gsl( gsl_rng_mt19937, seed );
103 
104 #else
105 
106  Seed1 = seed;
107 
108 #endif
109  }
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
void message(const char *message,...)
Definition: output.cc:40

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

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

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.

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

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.

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

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.

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

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.

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

References ScrambleArrayUInt(), and Uniform().

◆ Poisson()

static double RAND::Poisson ( double  mean)
inlinestatic

From the Numerical Recieps.

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

References gammln(), and Uniform().

Referenced by LCE_Breed_base::getPoissonFecundity(), TTDispersal::mutate(), TT_BDMI::mutate_diplo(), TT_BDMI::mutate_haplo(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), LCE_Patch_Extinction::rand_poisson(), GeneticMap::recombine(), ParamsParser::rpoiss(), and LCE_Breed_Disperse::stochasticLogisticGrowth().

◆ RandBool()

static bool RAND::RandBool ( )
inlinestatic

Returns a random boolean.

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

References Uniform().

Referenced by LCE_Patch_Extinction::do_remove(), TProtoQuanti::getMutationEffectBivariateDiallelic(), LCE_Breed_base::getOffsprgSexFixed(), LCE_Breed_base::getOffsprgSexRandom(), TTDispersal::inherit(), TTNeutralGenes_bitstring::inherit(), TProtoBDMI::inherit_free(), TProtoDeletMutations_bitstring::inherit_free(), TProtoQuanti::inherit_free(), TProtoNeutralGenes::inherit_free(), TTNeutralGenes_bitstring::init_sequence(), 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(), TTQuanti_diallelic_bitstring_no_pleio_epistasis::init_sequence(), TTQuanti_diallelic_bitstring_full_pleio_epistasis::init_sequence(), LCE_Disperse_EvolDisp::Migrate_SteppingStone1D(), TTDispersal::mutate(), TTNeutralGenes_bitstring::mutate(), TTNeutralGenes_byte::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_byte::mutate_KAM(), TProtoQuanti::mutate_no_pleio(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes_byte::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.

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

References Uniform().

Referenced by TTNeutralGenes_bitstring::inherit().

◆ 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
717  {
718  assert(from < to);
719 
720  unsigned int seq_length = to - from; //don't include last (to)
721 
722  assert(num <= seq_length);
723 
724  // we build a sequence of indexes to choose from
725  int *seq = new int [seq_length];
726 
727  seq[0] = from;
728 
729  for(unsigned int i = 1; seq[i-1] < to && i < seq_length; ++i)
730  seq[i] = seq[i-1] + 1;
731 
732  if(!replace) { //without replacement
733 
734  unsigned int size = seq_length, pos, last = seq_length - 1;
735 
736  for (unsigned int i = 0; i < num; i++) {
737  pos = RAND::Uniform(size);
738  result[i] = seq[pos];
739  seq[pos] = seq[last];
740  // seq[last] = result[i]; useless operation
741  size--; last--;
742  }
743 
744  } else { //with replacement
745  for (unsigned int i = 0; i < num; i++)
746  result[i] = seq[ RAND::Uniform(seq_length) ];
747  }
748 
749  delete [] seq;
750  }

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

References Uniform().

◆ SampleSeqWithReciprocal()

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

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
693  {
694  unsigned int size = length, pos, last = length - 1, num = length -1, el;
695 
696  // with stop after 2 elements are left
697  // this algo allows swapping an element with itself, which is fine
698  for (unsigned int i = 0; i < num; i++) {
699  pos = RAND::Uniform(size);
700  el = array[last];
701  array[last] = array[pos];
702  array[pos] = el;
703  size--; last--;
704  }
705 
706  assert(size == 1); //we stopped before swapping the last (first) element with itself
707  }

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.

127  {
128 
129 #ifdef HAS_SPRNG
130  return stream->sprng();
131 #elif defined(HAS_GSL)
132  return gsl_rng_uniform(r);
133 #else
134  register long z, w;
135 
136  do{
137  w = Seed1 / 53668;
138 
139  Seed1 = 40014 * (Seed1 - w * 53668) - w * 12211;
140 
141  if (Seed1 < 0) Seed1 += 2147483563;
142 
143  w = (Seed2 / 52774);
144 
145  Seed2 = 40692 * (Seed2 - w * 52774) - w * 3791;
146 
147  if (Seed2 < 0) Seed2 += 2147483399;
148 
149  z = Seed1 - Seed2;
150 
151  if (z < 1) z += 2147483562;
152 
153  }while (!((z * 4.656613e-10) < 1.0));
154 
155  return (z * 4.656613e-10);
156 #endif
157  }
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_byte::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(), TTNeutralGenes_bitstring::mutate(), TTWolbachia::mutate(), TTNeutralGenes_byte::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_byte::mutate_KAM(), TProtoQuanti::mutate_no_pleio(), TTDeletMutations_bitstring::mutate_noredraw(), TTDeletMutations_bitstring::mutate_noredraw_noBackMutation(), TTDeletMutations_bitstring::mutate_redraw(), TTNeutralGenes_byte::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[.

160  {
161  return (unsigned int)(Uniform() * max);
162  }

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