Random numbers in C

后端 未结 10 1413
栀梦
栀梦 2020-12-07 01:26
for(i = 0; i < n; i++){
        srand(time(NULL));
        printf(\"%d \", time(NULL));
        for(j = 0; j < (n-1); j++){
            a[i][j] = rand();
              


        
10条回答
  •  星月不相逢
    2020-12-07 01:50

    Call srand() outside of the loop. You are reseeding it every iteration.

    srand() seeds the random number generator so you get a different sequence of random numbers depending on the input. Your loop runs very fast, so the call to time(NULL) always returns the same value. You are resetting to the same random sequence with every iteration. As a general rule, only call srand() once in your program.

提交回复
热议问题