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

#include <math.h>

#define PI 3.14159265359

// 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 * PI * v;
  *z0            = r * cos(x);
  *z1            = r * sin(x);
}

double normal_transform(double z, double mu, double sigma) {
  return z * sigma + mu;
}