aboutsummaryrefslogtreecommitdiff
path: root/random/src/normal.c
blob: d4bcac1e114e405ce866862c12bfe5a25180bfa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <random/normal.h>

#include <math.h>

// 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;
}