-
Notifications
You must be signed in to change notification settings - Fork 0
/
randnorm.c
25 lines (23 loc) · 855 Bytes
/
randnorm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/***************************************************************/
/* Program: randnorm.c
By: Brad Duthie
Description: Will produce one norm distributed random number
Compile: gcc randnormINT.c -ansi -Wall -pedantic */
/***************************************************************/
#include<stdio.h> /* Standard input/output */
#include<stdlib.h> /* Standard library */
#include<math.h> /* Math library (for log and sqrt) */
#include "randunif.h" /* Need to generate random uniforms */
double randnorm(double mean, double sd){
double x1, x2, w, y;
do{
x1 = 2.0 * randunif() - 1;
x2 = 2.0 * randunif() -1;
w = x1*x1 + x2*x2;
} while(w >= 1.0);
w = sqrt((-2.0 * log(w)) / w);
y = x1*w;
y = y*sd;
y = y + mean;
return y;
}