In my prev. posting I forgot to mention that the random numbers shall be
in the range 0 to 99. So here my updated question:
What is the correct method for generating 2 independent random numbers
in the range 0 to 99 ? They will immediately be compared for equality.
As others have mentioned, most random number generators (and almost
certainly your `rand()' call) generate *pseudo*-random sequences of
numbers. "Real" random number sequences (and it's by no means trivial to
say what that even means) are hard to come by and will generally involve
some hardware-based solution. I'll assume that pseudo random is good
enough for your purposes.
But be aware that there is a huge variation in the "randomness" of
sequences generated by pseudo random generators. The system-supplied
generators (like `rand()') are often astonishingly non-random, and the
(current) C++ standard allows them to be as cruddy as they please.
Thankfully, the forthcoming C++ standard will include some higher quality
generators such as the Mersenne Twister (recommended, Google it). But
even that would not be good enough e.g. for cryptographic purposes.
What about this method:
srand(time(0));
This is ok as long as you don't call this routine again so quickly that
time(0) returns the same value as for the last call.
This can be a bad idea, particularly for so-called "linear congruential"
rngs (and there's a fair chance your `rand()' will be linear
congruential). The reason for this is that mod-ing a number basically
involves using just the lower-order bits, which for many rngs - and
linear congruential ones in particular - are frequently the "least
random".
Safer is something along the lines of:
int r1 = int(100.0*double(rand())/double(RAND_MAX));
that is, you generate a uniform(ish) random double in the range [0,1),
multiply it by 100 and take the integer part. This gives you a uniform
(ish) random int in the range [0,99). It's safer because it relies more
heavily on the higher-order bits of the number returned by your rng.
^^^^^^^^^^^^^^
Probably a bad idea to re-seed your rng with a number generated by the
same rng... and pointless in any case. Seed once and generate away...
Leave out that line.
Again, as for r1 above.