Don't leave randomness to chance (or, avoid using rand if it matters)

A. Sinan Unur

Just a little caution.

What do you think this piece of code prints?

use strict; use warnings;
my %r = map { rand() => undef } 1 .. 1_000_000;
print scalar keys %r, "\n";

On my Windows system with both ActiveState perl 5.10.1 and Strawberry perl 5.12.1, it prints 32768.

That is, the default random number generator on this system will only ever give you 32,768 distinct values.

Use a module such as Math::Random when randomness matters.

Now, your system might be better. You can feel good about that.

However, the point of this caution is to alert once more all those programmers who think all rands are created equal.

They are not.

Do not leave things to chance when randomness matters.

Use a well-known pseudo random number generator with established properties.

Not any odd function called rand which the default runtime gives you.