diff options
Diffstat (limited to 'random/src')
-rw-r--r-- | random/src/normal.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/random/src/normal.c b/random/src/normal.c index d4bcac1..5978274 100644 --- a/random/src/normal.c +++ b/random/src/normal.c | |||
@@ -2,16 +2,18 @@ | |||
2 | 2 | ||
3 | #include <math.h> | 3 | #include <math.h> |
4 | 4 | ||
5 | #define PI 3.14159265359 | ||
6 | |||
5 | // Generate two samples in the standard normal distribution using the | 7 | // Generate two samples in the standard normal distribution using the |
6 | // Box-Muller transform. | 8 | // Box-Muller transform. |
7 | // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform | 9 | // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform |
8 | void normal2(double u, double v, double* z0, double* z1) { | 10 | void normal2(double u, double v, double* z0, double* z1) { |
9 | const double r = sqrt(-2 * log(u)); | 11 | const double r = sqrt(-2 * log(u)); |
10 | const double x = 2 * M_PI * v; | 12 | const double x = 2 * PI * v; |
11 | *z0 = r * cos(x); | 13 | *z0 = r * cos(x); |
12 | *z1 = r * sin(x); | 14 | *z1 = r * sin(x); |
13 | } | 15 | } |
14 | 16 | ||
15 | double normal_transform(double z, double mu, double sigma) { | 17 | double normal_transform(double z, double mu, double sigma) { |
16 | return z*sigma + mu; | 18 | return z * sigma + mu; |
17 | } | 19 | } |