GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
gauss.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <grass/gmath.h>
3 
4 
5 /*!
6  * \fn double G_math_rand_gauss(const int seed, const double sigma)
7  *
8  * \brief Gaussian random number generator
9  *
10  * Gaussian random number generator (mean = 0.0)
11  *
12  * \param[in] seed
13  & \param[in] sigma
14  * \return double
15  */
16 
17 double G_math_rand_gauss(double sigma)
18 {
19  double x, y, r2;
20 
21  do {
22  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
23  x = -1 + 2 * G_math_rand();
24  y = -1 + 2 * G_math_rand();
25 
26  /* see if it is in the unit circle */
27  r2 = x * x + y * y;
28  }
29  while (r2 > 1.0 || r2 == 0);
30 
31  /* Box-Muller transform */
32  return sigma * y * sqrt(-2.0 * log(r2) / r2);
33 }
double G_math_rand_gauss(double sigma)
Definition: gauss.c:17
float G_math_rand(void)
Definition: rand1.c:17
#define x