Hello. I recently tried to create a mini program. I wanted to use a command that works something like a dice.
If it rolled on ONE, it would activate the command I had for ONE.
If it rolled on TWO, it would activate the command I had for TWO.
etc, etc...
rand itself puts out a number between 1 and RAND_MAX, which is a constant defined in cstdlib. The RAND_MAX is much bigger than 100, but you get the output you want by using the modulo (%6 in your case). This will cause the number rand creates to be divided by 6 and the remainder is what is stored. Thus the number will be between 0-5. Since you want 1-6 all you have to do is put int random_number = rand() % 6 + 1, which will create a random number, take the remainder between 0-5, and add 1.
edit: oops, hamsterman is of course correct. What I said in the first place about %6 being what you want is the correct solution. I'll fix that.
Yep, it's a cool little thing. And Just to clarify, it's not specific to the rand() function, you can use it with any number or function that returns a number.
Well, I'm not sure about rand(), but what happened in QuickBASIC was that (aside from the obvious performance impact) the numbers lost randomness. When I plotted them to the screen they seemed to prefer certain lines. This most likely happens because the random value now depends on the value the system clock happens to have at the time of the call, instead of depending on the random value that came before it.
By the way, I hope we all know by now that these are pseudo-random numbers (unless rand() has been specially crafted to use a system-specific device, such a broken radio, to get its random values).