Last active
September 25, 2015 04:03
-
-
Save MirrorDM/abceac811b8a5b0ffd4f to your computer and use it in GitHub Desktop.
Generating Gaussian Random Numbers. Implements the Polar form of the Box-Muller Transformation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* boxmuller.c Implements the Polar form of the Box-Muller | |
Transformation | |
(c) Copyright 1994, Everett F. Carter Jr. | |
Permission is granted by the author to use | |
this software for any application provided this | |
copyright notice is preserved. | |
*/ | |
#include <math.h> | |
extern float ranf(); /* ranf() is uniform in 0..1 */ | |
float box_muller(float m, float s) /* normal random variate generator */ | |
{ /* mean m, standard deviation s */ | |
float x1, x2, w, y1; | |
static float y2; | |
static int use_last = 0; | |
if (use_last) /* use value from previous call */ | |
{ | |
y1 = y2; | |
use_last = 0; | |
} | |
else | |
{ | |
do { | |
x1 = 2.0 * ranf() - 1.0; | |
x2 = 2.0 * ranf() - 1.0; | |
w = x1 * x1 + x2 * x2; | |
} while ( w >= 1.0 ); | |
w = sqrt( (-2.0 * log( w ) ) / w ); | |
y1 = x1 * w; | |
y2 = x2 * w; | |
use_last = 1; | |
} | |
return( m + y1 * s ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment