Random numbers in C

后端 未结 10 1411
栀梦
栀梦 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:56

    You need to call srand() before entering the loop. srand() initializes the radnom number generator with the given seed and generates unique sequence of random numbers for this seed.

    Your loop executes very fast so every call to time(NULL) yields the same time (measured in seconds) - hence you initialize random number generator with the same seed on every loop iteration.

提交回复
热议问题