/* dboard.c
see pi_send.c and pi_reduce.c */
#include <stdlib.h>
#define sqr(x) ((x)*(x))
//long rand(void);
double dboard(int darts)
{
double x_coord, /* x coordinate, between -1 and 1 */
y_coord, /* y coordinate, between -1 and 1 */
pi, /* pi */
r; /* random number between 0 and 1 */
int score, /* number of darts that hit circle */
n;
score = 0;
/* "throw darts at board" */
for (n = 1; n <= darts; n++) {
/* generate random numbers for x and y coordinates */
r = (double)rand()/(double)RAND_MAX;
x_coord = (2.0 * r) - 1.0;
r = (double)rand()/(double)RAND_MAX;;
y_coord = (2.0 * r) - 1.0;
/* if dart lands in circle, increment score */
if ((sqr(x_coord) + sqr(y_coord)) <= 1.0)
score++;
}
/* calculate pi */
pi = 4.0 * (double)score/(double)darts;
return(pi);
}