C++ random numbers

前端 未结 7 1070
生来不讨喜
生来不讨喜 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:23

    • When you perform "modulo" arithmetic you get the remainder when dividing. So rand() gives you an integer and rand() % 10 is a number between 0 and 9. Add 1 to get a number in the range 1 to 10

    • Random number generators will always generate the same sequence of numbers unless you seed them first. srand(time(0)) "seeds" the random generator with a number based upon the current time in seconds. The theory is that you will run this at different times thus seeding it differently each time, and thus you will get a different sequence of numbers each time the program is run.

提交回复
热议问题