- #1
vladittude0583
- 40
- 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
#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