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
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.