-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormal.c
More file actions
32 lines (28 loc) · 702 Bytes
/
normal.c
File metadata and controls
32 lines (28 loc) · 702 Bytes
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
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
double rand_gen() {
// return a uniformly distributed random value
return ( (double)(rand()) + 1. )/( (double)(RAND_MAX) + 1. );
}
double normalRandom() {
// return a normally distributed random value
double v1=rand_gen();
double v2=rand_gen();
return cos(2*3.14*v2)*sqrt(-2.*log(v1));
}
main() {
FILE *file;
file = fopen("normal100.txt", "w");
if(file == NULL){
printf("Error!");
exit(1);
}
double sigma = 10.0;
double Mi = 50.0;
for(int i=0;i<100000;i++) {
double x = normalRandom()*sigma+Mi;
fprintf(file,"%d ",(int)x);
}
}