i hope you're having a nice day :D
As a coding project for highschool, i'm making a program which calculates an aproximation of pi number.
im using a method called monte carlo, where you need to generate random numbers to calculate it.
But the aproximation is ending up being very inexact ( it has a value of aproximatedly 3.13 instead of 3.141 for example)
i think the problem is that numbers generated aren't enough random, so i would like to know if there is a way of calculating more random numbers in c++. im already using rand() and random seed with time null at the beginning of the script. Thank you very ,much for your help!
here is my code (im sure its not perfect as im starting to program)
in = 1;
out = 1;
printf("introducir numero de iteraciones \n");
scanf("%ld", & intentos);
iterations = 1;
srand (time(NULL));
while (iterations < intentos) {
z = rand()%2000;
t = rand()%2000;
x = (((double) z )/ 1000) -1 ;
y = (((double) t )/ 1000) -1 ;
if( sqrt(pow(x,2.0) + pow(y,2.0)) <= 1 ){
in = in + 1;
}
iterations = iterations + 1;
}
pi = 4 * (in/iterations);
printf("\n \n para %ld iteraciones, el valor de pi es %f",iterations,pi);
Aucun commentaire:
Enregistrer un commentaire