Possible Duplicate:
Generating random integer from a range
I just started learning C++ and I’m trying to generate a random integer thats either 1, 2, or 3. I searched around and all the examples I see of generating random numbers are confusing and always different from the last example I looked at. Is there a simple way to do this?
The modulo solution is the most straightforward but it usually loses randomness as modulo as the tendency to “eat up” the lowest bits of the result.
A more random way is to map [0,1[ over [a,b[ in a linear way:
A generic version is trivially derived from these to get a roll( T min, T max) version.