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