Why Is My Pi Estimation Code Producing Incorrect Results?

AI Thread Summary
The code for estimating Pi using a dart simulation contains several errors, primarily in the handling of the random number generator and the formatting of output. The user mistakenly implemented their own version of the `srand` function, which is already provided by the standard C library. Additionally, there is a mismatch in the printf statement where five format specifiers are used for only four parameters, and the `estimatedPi` variable should be printed using the `%lf` format specifier instead of `%d`. After correcting these issues, the code produces accurate estimates of Pi with minimal error. The discussion highlights the importance of adhering to standard library functions and proper data type handling in C programming.
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
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...
Back
Top