简体   繁体   中英

Error with rand() function: same numbers into a for loop

I have a problem with the rand() function. I would like to randomly generate eps values, different one from each other for i=0,...,VOL. However, when I print eps, it is always the same. Could you please tell what it is wrong in my code? Thank you.

    ...
    #include <time.h>
    ...

    void function(...);

    int main(){

    function();

    return 0;
    }

    void function(...){

    srand((unsigned int)time(NULL));
    ...

    for(i=0;i<VOL;i++){

            signal1[i]=0.; // No signal

            eps=rand()/(RAND_MAX+0.5);

            if(signal1[i]==(MIN+MAX)){ 

                net[i]= 0; 

                exp[i]=a+eps; 
                printf("eps: %f\n", eps);

            } 
    }
    }

The full part of the code (to copy the entire code is impossible as it is very long) is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define VOL 15

#define MAX 10
#define MIN 0

float random_sign_high[VOL]={2,2,2,2,2,2,2,2,2,1,1,1,1,1,0};
float random_sign_low[VOL]={2,2,2,2,2,2,2,2,1,1,1,1,1,0,0};

void function();

int main(){

function();

return 0;
}

void function(){
...
srand((unsigned int)time(NULL));

for(i=0;i<VOL;i++){

        signal1[i]=0.; // No signal
        signal2[i]=0.; // No signal


        if(H_PR!=0){
             shuffle_signals(random_sign_high);
        }

        if(L_PR!=0){
            shuffle_signals(random_sign_low);
        }

        eps=rand()/(RAND_MAX+0.5);
        printf("eps: %f\n", eps);

        if(tot_sig==(MIN+MAX)){

            net[i]= 0;

            exp_p[i]=a+eps;

        }

and the shuffle function is:

double shuffle_signals(float array[VOL])

{
    srand((unsigned int) time(NULL));

    if(VOL>1)
    {
        int i,j,t;
        for(i=0; i<VOL;i++)
        {
            j=i+rand()/((float)RAND_MAX/(VOL-i)+1.);
            t=array[j];
            array[j]=array[i];
            array[i]=t;

            if(array[i]==1){
                signal2[i]=MIN;
                signal1[i]=MAX;
            }
            else if(array[i]==0){
                signal2[i]=MIN;
                signal1[i]=MIN;
            }
            else if (array[i]==2){
                signal1[i]=MAX;
                signal2[i]=MAX;
            }
            tot_sig= signal1[i]+signal2[i];
        }
        // printf("tot_sign: %lf\n", tot_sig);
    }
    return tot_sig;
}

}

The other parts are irrelevant. You can think 'a' be a constant, H_PR=0.5 and L_PR=0.1 Thanks a lot.

You're calling shuffle_signals() repeatedly from inside a loop. Each time you visit this function, you call srand() , which resets the random number generator based on the current time (seconds since 1970). You should only call srand() once in your program. Somewhere near the top of main() would be a good place to do it.

the function: shuffle_signals() is recursive, However, the function: srand() should be called only once in the whole program. Suggest moving the call to srand() to early in the main() function.

You can do that:

int main ()
{
    int x;
    x = rand() % 100; // here you got always the same value
    printf ("Our first number: %d\n", x);
    srand ( time(NULL) ); // from now on you'll get random values
    x = rand() % 100;
    printf ("Some random number: %d\n", x);
    x = rand() % 100;
    printf ("The first number again: %d\n", x);

    return 0;
}

There's nothing affects random values in when adding unsigned int in srand(unsigned int(time(NULL))) so you can add it or leave it the result is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM