Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
Uniform.h
Go to the documentation of this file.
1 
27 #ifndef __UNIFORM_H
28 #define __UNIFORM_H
29 #include <cmath>
30 #include <limits.h>
31 #include <assert.h>
32 #include <valarray>
33 #include "output.h"
34 #include <iostream>
35 #include "MPImanager.h"
36 
37 #ifdef HAS_SPRNG
38 // #define SIMPLE_SPRNG
39  #include <sprng_cpp.h>
40 #endif
41 
42 #ifdef HAS_GSL
43  #include <gsl/gsl_rng.h>
44  #include <gsl/gsl_randist.h>
45  #include <gsl/gsl_blas.h>
46  #include <gsl/gsl_math.h>
47  #include <gsl/gsl_vector.h>
48  #include <gsl/gsl_matrix.h>
49  #include <gsl/gsl_permutation.h>
50 #endif
51 
52 extern MPIenv *_myenv;
53 
55 class RAND {
56 private:
57 
58  RAND();
59 
60 public:
61 
62 #if defined(HAS_SPRNG)
63 
64  static Sprng *stream;
65 
66 #elif defined(HAS_GSL)
67 
68  static gsl_rng * r;
69 
70  static void init_gsl(const gsl_rng_type* T, unsigned long seed)
71  {
72  r = gsl_rng_alloc (T);
73  gsl_rng_set(r,seed);
74  }
75 
76 #else
77 
78  static long Seed1,Seed2;
79 
80 #endif
82  static void init (unsigned long seed) {
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  }
108  static void free()
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  }
118 
124  static inline double Uniform () {
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  }
156  static inline unsigned int Uniform (unsigned int max)
157  {
158  return (unsigned int)(Uniform() * max);
159  }
160 
162  static inline bool RandBool() {
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  }
189 
191  static inline unsigned long RandULong() {
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  }
204  static inline double gammln (double xx) {
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  }
219  static inline double Poisson (double mean) {
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  }
260 
261  static inline double Gaussian(double sigma)
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  }
329 
330  static inline void BivariateGaussian(double sigma1,double sigma2, double rho, double *out1,double *out2) {
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  }
356 
357  static inline double LogNormal (double zeta, double sigma) {
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  }
384 
385  static inline double Gamma (double a, double b) {
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  }
428 
429  static inline double Bernoulli (double p)
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  }
446 
447  static inline double Exponential (double mu) {
448  return -mu * log(RAND::Uniform());
449  }
450 
451 #ifdef HAS_GSL
452  static inline void MultivariateGaussian(gsl_vector *eval, gsl_matrix *evec,
453  gsl_vector *workspace, gsl_vector *px)
454  {
455  size_t i;
456 
457  for (i=0; i<eval->size; i++)
458  gsl_vector_set (workspace, i,
459  Gaussian(gsl_vector_get (eval, i)));
460 
461  gsl_blas_dgemv (CblasNoTrans, 1.0,
462  evec, workspace, 0.0, px); /* px = evec * px */
463 
464  }
465 
466  static inline void MultivariateGaussianCholesky(gsl_vector *sigma, gsl_matrix *M, gsl_vector *px)
467  {//generate n i.i.d. random normal variates
468  for (size_t i = 0; i < sigma->size; i++)
469  gsl_vector_set (px, i, Gaussian(gsl_vector_get(sigma, i)));
470 
471  gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, M, px);
472  }
473 
474  static inline long double factoriel(unsigned int x) {
475  long double f = 1;
476  for(unsigned int i=1;i<=x;++i)
477  f *= i;
478  return f;
479  }
480 
481 // static inline double Binomial (double p,unsigned int k,long double N,
482 // const unsigned int n) {
483 // register long double bincoeff = N/(factoriel(k)*factoriel(n - k));
484 // return (bincoeff*pow(p,(int)k)*pow(1-p,(int)(n - k)));
485 // }
486 
487 #endif
488 
489  static inline double Binomial(double p, unsigned int n)
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  }
525 
526  static inline double Beta (const double a, const double b)
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  }
534 
535  static inline unsigned int Binomial2 (double p, unsigned int n){
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  }
543 
544  static inline void Multinomial (size_t K, unsigned int N, const double p[], unsigned int n[])
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  }
584  static inline void MultinomialOnNormalizedValarray (size_t K, unsigned int N, const std::valarray< double > &p,
585  unsigned int n[])
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  }
600 
602  static inline void MultinomialOnNormalizedValarray_expandedOut (size_t K, unsigned int N,
603  const std::valarray<double> &p,
604  unsigned int 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  }
625 
627  static inline void MultinomialOnNormalizedValarray_scrambleOut (size_t K, unsigned int N,
628  const std::valarray<double> &p,
629  unsigned int 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  }
654 
656  static inline void MultinomialOnNormalizedValarrayZipper_scrambleOut (size_t K, unsigned int N,
657  const std::valarray<double> &p,
658  unsigned int 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  }
684 
689  static inline void ScrambleArrayUInt (const int length, unsigned int *array)
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  }
705 
713  static inline void Sample (const int from, const int to, const unsigned int num, int* result, bool replace)
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  }
748 
749  static inline void SampleSeq(int from, int to, int by, unsigned int num, int* result, bool replace = false)
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  }
781 
782  static inline void SampleSeqWithReciprocal(int from, int to, int by, unsigned int num1, int* result1, unsigned int num2, int* result2)
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  }
811 
813  static inline size_t Discrete (const gsl_ran_discrete_t *g)
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  }
844 
845 };
846 
847 #endif
MPIenv * _myenv
Definition: MPImanager.cc:36
MPI environment setup.
Definition: MPImanager.h:122
int workerCount() const
Definition: MPImanager.h:129
int workerRank() const
Definition: MPImanager.h:130
Random number generation class, uses various types of random generators depending on the implementati...
Definition: Uniform.h:55
static double Exponential(double mu)
Definition: Uniform.h:447
static double Bernoulli(double p)
Definition: Uniform.h:429
static void SampleSeqWithReciprocal(int from, int to, int by, unsigned int num1, int *result1, unsigned int num2, int *result2)
Definition: Uniform.h:782
static double gammln(double xx)
From the Numerical Recieps.
Definition: Uniform.h:204
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.
Definition: Uniform.h:584
static void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[])
Definition: Uniform.h:544
static void BivariateGaussian(double sigma1, double sigma2, double rho, double *out1, double *out2)
Definition: Uniform.h:330
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 siz...
Definition: Uniform.h:656
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 siz...
Definition: Uniform.h:602
static double Binomial(double p, unsigned int n)
Definition: Uniform.h:489
static void free()
Memory de-allocation.
Definition: Uniform.h:108
static long Seed1
Definition: Uniform.h:78
static void ScrambleArrayUInt(const int length, unsigned int *array)
Randomize the elements within an array.
Definition: Uniform.h:689
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:219
static unsigned int Binomial2(double p, unsigned int n)
Definition: Uniform.h:535
static size_t Discrete(const gsl_ran_discrete_t *g)
Calling the GSL ran_discrete function.
Definition: Uniform.h:813
static unsigned long RandULong()
Return a random unsigned long, from uniform distribution.
Definition: Uniform.h:191
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 siz...
Definition: Uniform.h:627
static unsigned int Uniform(unsigned int max)
Returns a uniformly distributed random number from [0.0, max[.
Definition: Uniform.h:156
static void init(unsigned long seed)
Initialize the random generator's seed.
Definition: Uniform.h:82
static long Seed2
Definition: Uniform.h:78
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.
Definition: Uniform.h:713
static double Beta(const double a, const double b)
Definition: Uniform.h:526
static double Gaussian(double sigma)
Definition: Uniform.h:261
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:124
static void SampleSeq(int from, int to, int by, unsigned int num, int *result, bool replace=false)
Definition: Uniform.h:749
static bool RandBool()
Returns a random boolean.
Definition: Uniform.h:162
static double LogNormal(double zeta, double sigma)
Definition: Uniform.h:357
static double Gamma(double a, double b)
Definition: Uniform.h:385

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