forked from olcf/Serial-to-Parallel--Monte-Carlo-Pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialpi.c
29 lines (26 loc) · 854 Bytes
/
serialpi.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
27
28
29
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main(int argc, char* argv[])
{
double niter = 100000;
double x,y; //x,y value for the random coordinate
int i;
int count=0; //Count holds all the number of how many good coordinates
double z; //Used to check if x^2+y^2<=1
double pi; //holds approx value of pi
time_t t;
srand48(((unsigned)time(&t))); //Give rand() a seed value
for (i=0; i<=niter; ++i) //main loop
{
x = (double)drand48();//RAND_MAX; //gets a random x coordinate
y = (double)drand48();//RAND_MAX; //gets a random y coordinate
z = ((x*x)+(y*y)); //Checks to see if number in inside unit circle
if (z<=1)
{
++count; //if it is, consider it a valid random point
}
}
pi = ((double)count/(double)niter)*4.0; //p = 4(m/n)
printf("Pi: %f\n", pi);
}