From 685950d3f2ac8e893a23443f9e087294cce2c6fa Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Wed, 11 May 2022 09:56:33 -0700 Subject: Add random variable. --- random/src/normal.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 random/src/normal.c (limited to 'random/src') 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 @@ +#include + +#include + +// Generate two samples in the standard normal distribution using the +// Box-Muller transform. +// https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform +void normal2(double u, double v, double* z0, double* z1) { + const double r = sqrt(-2 * log(u)); + const double x = 2 * M_PI * v; + *z0 = r * cos(x); + *z1 = r * sin(x); +} + +double normal_transform(double z, double mu, double sigma) { + return z*sigma + mu; +} -- cgit v1.2.3