+ 1
How can I make it generate a new number each time the function runs?
https://code.sololearn.com/cy871Q2D6rny/?ref=app So if I run r() twice like I did, it should generate 2 different numbers. Why doesn't it?
3 ответов
+ 12
Daniel Cooper use srand function within main function gives you different sequence on every run.
https://code.sololearn.com/cd78tkZhFKD5/?ref=app
+ 4
1) What GAWEN STEASY said.
2) The reason is that srand() initializes the pseudo number generator with a seed. If you use the same seed, the same set of "random" numbers will be generated. In your code, you use the second as a seed. If you call r() two times in a row, there's an almost 100% chance that the function will be executed within the same second. So you re-initialize the pseudo number generator with the same seed and generate the same set of "random" numbers again.
+ 2
Thanks GAWEN STEASY