GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <math.h> 00002 #include <grass/gmath.h> 00003 00004 00017 double G_math_rand_gauss(const int seed, const double sigma) 00018 { 00019 double x, y, r2; 00020 00021 do { 00022 /* choose x,y in uniform square (-1,-1) to (+1,+1) */ 00023 x = -1 + 2 * G_math_rand(seed); 00024 y = -1 + 2 * G_math_rand(seed); 00025 00026 /* see if it is in the unit circle */ 00027 r2 = x * x + y * y; 00028 } 00029 while (r2 > 1.0 || r2 == 0); 00030 00031 /* Box-Muller transform */ 00032 return sigma * y * sqrt(-2.0 * log(r2) / r2); 00033 }