C++ random numbers

前端 未结 7 1068
生来不讨喜
生来不讨喜 2021-01-20 05:55

At the following: http://www.fredosaurus.com/notes-cpp/misc/random.html

It mentions that if we want to generate a random number in the range 1-10, we ca

相关标签:
7条回答
  • 2021-01-20 06:46

    The modulo part is answered by others. You must initialize with srand(time(0)) because otherwise you will get the same series of random numbers each time your program runs.

    As far as I understand it, this is because the generator starts with a certain number and following numbers are calculated based on this starting number.

    You can initialise with any number you like, for example for testing you could use srand(0) and get the same numbers as in the last run. To get a fully random series you use time(0) because that initialises with the actual time in seconds Which is random enough for most purposes.

    0 讨论(0)
提交回复
热议问题