QTrk
Functions
random_distr.h File Reference

Go to the source code of this file.

Functions

template<typename T >
rand_uniform ()
 
template<typename T >
rand_normal ()
 
template<typename T >
int rand_poisson (T lambda)
 

Function Documentation

§ rand_normal()

template<typename T >
T rand_normal ( )

Definition at line 11 of file random_distr.h.

12 {
13  T U0 = rand_uniform<T>()+(T)1e-9;
14  T U1 = rand_uniform<T>();
15 
16  // Box-Muller transform
17  return sqrt( -2 * log(U0) ) * cos(2*(T)3.141593 * U1);
18 }
vector3< T > sqrt(const vector3< T > &a)
Definition: std_incl.h:112

§ rand_poisson()

template<typename T >
int rand_poisson ( lambda)

Definition at line 21 of file random_distr.h.

22 {
23  if (lambda > 10) {
24  T v = rand_normal<T>();
25  v = (T)0.5 + lambda + v*sqrt(lambda);
26  return (int)std::max((T)0,v);
27  }
28  else {
29  T L = exp(-lambda);
30  int k = 0;
31  T p = 1;
32  do {
33  k++;
34  T u = rand_uniform<T>();
35  p = p * u;
36  } while (p > L && k<200);
37 
38  // copy back
39  return k-1;
40  }
41 }
vector3< T > sqrt(const vector3< T > &a)
Definition: std_incl.h:112

§ rand_uniform()

template<typename T >
T rand_uniform ( )

Definition at line 4 of file random_distr.h.

5 {
6  return rand() / (T)RAND_MAX;
7 }