0

I want to generate two random arrays no matter what i tried but the two tables are always identical

double *init_rand_w(double tableau[],int tailleTableau) {
    //double *tableau=malloc(tailleTableau*sizeof(double));
    srand(time(NULL));

    for (int i = 0 ; i < tailleTableau ; i++)
    {

        tableau[i]=((double)rand())/((double)RAND_MAX);
    }

    return  tableau;
}

int main()
{
    double *t1=(double*)malloc(sizeof(double)*10);
    double *t2=(double*)malloc(sizeof(double)*10);

    t1=init_rand_w(t1,5);
    t2=init_rand_w(t2,5);

    shuffle(t2,5);

    printf("***************************\n");

    for (int x = 0; x <5; x++)
    {
        printf("%f  ,  %f \n", t1[x],t2[x]);
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53

1 Answers1

1

You should just call srand once at the beginning of your program. It initializes the state of the random number generator. You are currently calling it twice, and you are likely giving it the same seed value each time you call it (since your program would take a lot less than one second to run), so you would get the same sequence of random numbers.

David Grayson
  • 84,103
  • 24
  • 152
  • 189