Browse Source

Add random variable.

main
Marc Sunet 7 months ago
parent
commit
685950d3f2
  1. 3
      random/CMakeLists.txt
  2. 9
      random/include/random/normal.h
  3. 2
      random/include/random/random.h
  4. 17
      random/src/normal.c

3
random/CMakeLists.txt

@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 3.0)
project(random)
add_library(random
src/mt19937-64.c)
src/mt19937-64.c
src/normal.c)
target_include_directories(random PUBLIC include)

9
random/include/random/normal.h

@ -0,0 +1,9 @@
#pragma once
/// Generate two samples from the standard normal distribution.
///
/// |u| and |v| must be uniformly distributed in (0,1).
void normal2(double u, double v, double* z0, double* z1);
/// Map a sample from a standard normal distribution to an arbitrary normal.
double normal_transform(double z, double mu, double sigma);

2
random/include/random/random.h

@ -1,3 +1,5 @@
#pragma once
#include <random/mt19937-64.h>
#include <random/normal.h>

17
random/src/normal.c

@ -0,0 +1,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;
}
Loading…
Cancel
Save