// Pi-estimating program with darts
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
#include <mpi.h>
int main(){
//Seed the RNG
//How many darts landed within the dartboard
int darts_within_dartboard = 0;
//How many darts landed within the dartboard
int darts_to_throw = 1e6;
for(int i =0; i < darts_to_throw; i++)
double rand_x
= ((double)rand() / RAND_MAX
) * 2.0 - 1.0; double rand_y
= ((double)rand() / RAND_MAX
) * 2.0 - 1.0;
double dist_squared = rand_x * rand_x + rand_y * rand_y;
if(dist_squared<= 1){
darts_within_dartboard += 1;
}
}
double pi_est = 4.0 * darts_within_dartboard / darts_to_throw;
printf("The estimated value of pi is: %f\n", pi_est
);
return 0;
}
Ly8gUGktZXN0aW1hdGluZyBwcm9ncmFtIHdpdGggZGFydHMKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlPHRpbWUuaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlIDxtcGkuaD4KCmludCBtYWluKCl7CiAgCiAgLy9TZWVkIHRoZSBSTkcKICBzcmFuZCh0aW1lKE5VTEwpKTsKICAKICAvL0hvdyBtYW55IGRhcnRzIGxhbmRlZCB3aXRoaW4gdGhlIGRhcnRib2FyZAogIGludCBkYXJ0c193aXRoaW5fZGFydGJvYXJkID0gMDsKICAKICAvL0hvdyBtYW55IGRhcnRzIGxhbmRlZCB3aXRoaW4gdGhlIGRhcnRib2FyZAogIGludCBkYXJ0c190b190aHJvdyA9IDFlNjsKICAKICBmb3IoaW50IGkgPTA7IGkgPCBkYXJ0c190b190aHJvdzsgaSsrKQogICAgCiAgICAKICAgIGRvdWJsZSByYW5kX3ggPSAoKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWCkgKiAyLjAgLSAxLjA7CiAgICBkb3VibGUgcmFuZF95ID0gKChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVgpICogMi4wIC0gMS4wOwogICAgCiAgICAKICAgIAogICAgCiAgICAKICAgIAogICAgZG91YmxlIGRpc3Rfc3F1YXJlZCA9IHJhbmRfeCAqIHJhbmRfeCArIHJhbmRfeSAqIHJhbmRfeTsKICAgIGlmKGRpc3Rfc3F1YXJlZDw9IDEpewogICAgICAKICAgICAgZGFydHNfd2l0aGluX2RhcnRib2FyZCArPSAxOwogICAgfQp9CgoKZG91YmxlIHBpX2VzdCA9IDQuMCAqIGRhcnRzX3dpdGhpbl9kYXJ0Ym9hcmQgLyBkYXJ0c190b190aHJvdzsKcHJpbnRmKCJUaGUgZXN0aW1hdGVkIHZhbHVlIG9mIHBpIGlzOiAlZlxuIiwgcGlfZXN0KTsKCnJldHVybiAwOwp9Cg==