Why Is My Pi Estimation Code Producing Incorrect Results?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
vladittude0583
Messages
40
Reaction score
0
Here is what I have coded so far:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int randomNumber(void); // Function prototype for random number generator
void srand(unsigned int seed); // Function prototype for seeding the RNG

int main()
{
// Declaration of double variables

double x; // coordinate of dart
double y; // coordinate of dart
double v; // coordinate of dart
double estimatedPi;
double Pi;

// Declaration of long variables

long startValue;
long seed;
long impact;
long target;

// Declaration of initialization values

startValue = 100;
seed = time(NULL);
impact = 0;
target = 0;
Pi = 3.14159265358979;

srand(seed);

printf("\t\t\tPi Dart Simulator v1.2 \n\n");
printf("True Pi to 8 decimal places: 3.14159265358979 \n");
printf("Random number seed: 1225850161 \n\n");

printf(" Throws Hits Esimated Pi %%Error \n");
printf("====================================================\n\n");

while (impact < startValue && impact < 10000000)
{
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
impact += 1;

if ((sqrt(x*x + y*y)) <= 1.00)
{
target += 1;
estimatedPi = ((double)target / (double)impact)*4;
}

while (impact >= startValue)
{
printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
(((estimatedPi) - Pi) / Pi)*100);
startValue *= 10;
impact = target = 0;
}
}

system("pause");
return 0;
}

unsigned long random = 1;

void srand(unsigned int seed)
{
random = seed;
}

int randomNumber()
{
random = random * 1103515245 + 12345;
return (unsigned int)(random / 65536) % 327768;
}


What I cannot figure out is why my values for the estimation of Pi and percent error is coming out wrong - PLEASE HELP
 
Physics news on Phys.org
I tested your code, and there are many errors. I'm very curious whether it could compile on your system. Which compiler did you use? 
from the
Code:
system("pause");
, I guess that you are using Microsoft windows.:smile:, and I'm not sure a it is a legal statement in gcc under linux.

Code:
int randomNumber(void); // Function prototype for random number generator
What does this function use for?

Code:
void srand(unsigned int seed); // Function prototype for seeding the RNG
srand() is a function supplied by standard c library. So, you needn't implement it yourself.

Code:
printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
(((estimatedPi) - Pi) / Pi)*100);
You have Five "%", but you have only four associated parameters. And there is a mismatch of types. Because " estimatedPi " is declared as a double format variable, you should use "%lf" instead. So as the expression "(((estimatedPi) - Pi) / Pi)*100)".

After modifying the whole bugs, it works.
Here is the result in my system
Code:
Pi Dart Simulator v1.2

True Pi to 8 decimal places: 3.14159265358979
Random number seed: 1225850161

 Throws Hits Esimated Pi %Error
========================================== ==========

       100         78   3.120000  -0.687316
      1000        811   3.244000   3.259727
     10000       7793   3.117512  -0.766519
    100000      78629   3.145191   0.114553
   1000000     785078   3.140312  -0.040764
  10000000    7852331   3.140933  -0.021007