0

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);}
4
  • 1
    That's c and not c++. Jun 13, 2018 at 12:31
  • octal 1000 == decimal 512, and you're reading numbers as octal. Jun 13, 2018 at 12:36
  • 1
    I'm a total beginner in C++ From what I can see in your question, what little you learned is C not C++.
    – Borgleader
    Jun 13, 2018 at 12:38
  • You don't need any dynamic memory allocation as you only use 1 element at a time and free the memory afterwards. You could well do with a simple float variable instead of those arrays. Also your loops go beyond the end of your memory: (int i=0;i<j+2;i+=1) should be (int i=0; i<j; i+=1)
    – Gerhardh
    Jun 13, 2018 at 13:22

2 Answers 2

0
scanf("%lo",&j);

This interprets the number you enter as an octal integer, that is a number with only the digits 0 to 7.

Replace the o from the pattern with u.

scanf("%lu",&j);
0

Your issue is your format specifiers. The specifier "%lo" tells scanf to read a string representation of an unsigned octal integer and store it in the long object pointed to by the corresponding argument.

That means when you input "1000", its interpreted as octal, and 10008 is the same as 51210. You then add 2 to that number, which explains why you see 514 iterations for the input "1000".

Use "d" instead of "o" in your format specifiers to indicate a decimal integer:

scanf("%ld", &j);
1
  • Thank you a lot it was exactly that ! I didn't notice that "lo" was for "Long Octal". It works perfectly
    – Alx
    Jun 13, 2018 at 12:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.