Seeding the random number generator

2019-09-20 07:36发布

问题:

I don't recall seeing any program call srand with anything but srand(time(NULL)). Under what circumstances would one call srand with some other value?

回答1:

The function rand() doesn't return random numbers. It's a pseudo random generator instead. That's why you get the same sequence of numbers when you call srand() with the same argument. Calling srand()with the current time is the actually only random item. The numbers you get from rand() are just scrambled.

You will call srand() with a predictable or constant value when you want to replay a sequence, debug a functionality with the same sequence again and again.



标签: c++ c random