-
Notifications
You must be signed in to change notification settings - Fork 0
/
randnormINT.c
26 lines (24 loc) · 886 Bytes
/
randnormINT.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
26
/***************************************************************/
/* Program: randnormINT.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 */
int randnormINT(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;
y = floor(y + 0.5);
return y;
}