i'm trying to generate pseudo-random numbers with Park&Miller RNG ran0 1 2 3(3 doesn't work for the moment) in C++ from "Numerical Recipes in C". Theses generators work correctly because it makes samples uniformaly distributed between 0 and 1. My goal is to have a huge sample (10^10 at least) so i used malloc() function to allocate memory. My problem is that the sample is always to small (i'd like to have "j" random numbers generated, for example: if i initialize j=1000 i'll have 514 random numbers instead of 1000, in order to test things i made different "for" loops). I'm a total beginner in C++ (and in programming) and i fear a pointer problem, but i do not understand how to fix it. If someone has a solution or just a advice it would help me a lot because this problem seriously curb me in my internship (please be lenient with my approximative english :')). Here's the code:
int main(){
FILE* simulationr0 = fopen("E:/Simulation_stage/SimulationRan0","w");
FILE* simulationr1 = fopen("E:/Simulation_stage/SimulationRan1","w");
FILE* simulationr2 = fopen("E:/Simulation_stage/SimulationRan2","w");
FILE* simulationr3 = fopen("E:/Simulation_stage/SimulationRan3","w");
float d;
long j;
long seed;
printf("tapez 0 pour utiliser ran0\ntapez 1 pour utiliser ran1\ntapez 2 pour utiliser ran2\ntapez 3 pour utiliser ran3\ntapez 4 pour les utiliser tous");
scanf("%f",&d);
if( d!=0 & d!=1 & d!= 2 & d!= 3 & d!=4){
printf("Erreur: valeur incorrecte//incorrect value\n");
exit(0);}
printf("Combien de nombres pseudo-aleatoires ?// How many random numbers ?\n");
scanf("%lo",&j);
printf("Quelle graine ? Which seed ?\n");
scanf("%lo", &seed);
long *pseed=&seed;
if(d==4){
float * ranp0;
float * ranp1;
float * ranp2;
float * ranp3;
ranp0 = (float*) malloc (j*sizeof(float));
ranp1 = (float*) malloc (j*sizeof(float));
ranp2 = (float*) malloc (j*sizeof(float));
ranp3 = (float*) malloc (j*sizeof(float));
for (int i=0;i<j+2;i+=1){
ranp0[i]=ran0(pseed);
ranp1[i]=ran1(pseed);
ranp2[i]=ran2(pseed);
ranp3[i]=ran3(pseed);
fprintf(simulationr0,"%f\n", ranp0[i]);
fprintf(simulationr1,"%f\n", ranp1[i]);
fprintf(simulationr2,"%f\n", ranp2[i]);
fprintf(simulationr3,"%f\n", ranp3[i]);
}
fclose(simulationr0);
fclose(simulationr1);
fclose(simulationr2);
fclose(simulationr3);
system("PAUSE");
free(ranp0);
free(ranp1);
free(ranp2);
free(ranp3);}
if(d==0){
float * ranp0;
ranp0 = (float*) malloc (j*sizeof(float));
for (int i=0;i<j+2;i++){
ranp0[i]=ran0(pseed);
fprintf(simulationr0,"%f\n", ranp0[i]);}
fclose(simulationr0);
system("PAUSE");
free(ranp0);}
if(d==1){
float * ranp1;
ranp1 = (float*) malloc (j*sizeof(float));
for (int i=0;i<j+2;i++){
ranp1[i]=ran1(pseed);
fprintf(simulationr1,"%f\n", ranp1[i]);}
fclose(simulationr1);
system("PAUSE");
free(ranp1);}
if(d==2){
float * ranp2;
ranp2 = (float*) malloc(j*sizeof(long));
for (int i=0;i!=j;i=i+1){
ranp2[i]=ran2(pseed);
fprintf(simulationr2,"%f\n", ranp2[i]);}
fclose(simulationr2);
system("PAUSE");
free(ranp2);}
if(d==3){
float * ranp3;
ranp3 = (float*) malloc(j*sizeof(float));
for (int i=0;i<j+2;i++){
ranp3[i]=ran3(pseed);
fprintf(simulationr3,"%f\n", ranp3[i]);}
fclose(simulationr3);
system("PAUSE");
free(ranp3);}
system("PAUSE");
exit(1);}
Aucun commentaire:
Enregistrer un commentaire