rand() not generating random numbers even after srand(time(NULL))

后端 未结 2 1011
我寻月下人不归
我寻月下人不归 2021-01-29 11:47

I\'m trying to call a class function using a loop

for (int i = 0; i < Basket.getLemonNum(); i++)
{
    lemonWeights[i] = Fruit.generateWeight(fruit, fruitWeig         


        
2条回答
  •  清酒与你
    2021-01-29 12:27

    You must call srand() once, whereas you call it on every entry into generateWeight(). Since nowadays computers are fast and time() returns the time in seconds, this mostly restarts the random number generator from the same seed.

提交回复
热议问题