I'm making a dice game that generates random numbers from a dice. I'm reading from my book and I see " die1 = rand() % 6 + 1; ", I'm confused about what exactly does " + 1 " does. help me if you can please and thanks
I also need a clearer explanation on what " srand(time(0)); " and
" int rollDice(int num) " does
I know rand() % 6 outputs a random number thats greater than or equal to 0 and is less than 6...but what is the " + 1 " for
Because a dice cant be zero :D
if you use rand % 6, values can be
0,1,2,3,4,5
by adding 1, values can be
1,2,3,4,5,6
And srand is for seeding. If you dont seed, you will get same values every program run. But return value of time(null) is ALWAYS different. So your random values will be always different :)
The "+1" is because you will be wanting the dice roll to result in a value between 1 and 6, not 0 and 5 (the values which it will take if you don't add one).
"srand(time(0));" makes it so that a truly random number is generated each time (as if not, the program will generate one random number only and every time a random number is called, it will always be the same).
"int rollDice(int num)" is a function (I don't know what is inside it, but is sounds as though it generates a random number form 1 to 6, possibly "num" number of times, but I can't be sure a I can't see the code).
I may be able to tell you if you post the full "int rollDice(int num)" function (it should be followed by {///code\\\}, with "///code\\\" being some code to be run while the function is being run).