Prior to v5.20,
perl used whatever random number generator the system provided. This meant that the same program could have statistically different results based on the quality of that function. The
rand() for Windows had a max of 32,768 (15 bits), while POSIX has
drand48 (48 bits). This sort of numerical un-portability has always been a problem with
perl since it’s relied on the underlying libc for so much.
Not any more. It’s all internal to
perl now. With v5.20 and beyond, you’ll get the same pseudorandom number generator that everyone else with v5.20 and later gets.
For the effective programmer though, this doesn’t really matter because you shouldn’t be using the pseudorandom number generator for anything important. We call it
rand, but it’s not really. We should have called it
i_wont_install_a_module_so_ill_deal_with_it_random. The name in Perl comes from the name in libc (e.g. the GNU libc function list), just like many of the oddly named functions such as
getgrent. From the name, we get sloppy talking about it’s output as “random numbers” instead of the correct “pseudorandom” number.
Sinan Ünür examines how well Perl’s
rand does with coin flips and concludes it comes up short (Perl 5.20.0 brings a “better” PRNG to Windows). An older presentation from the Wellington Perl mongers goes through some serious math to talk about better pseudorandom numbers. The documentation for Math::Random::Secure has more interesting details. Several other modules provide
There are ways to get real random numbers. Atmospheric noise, nuclear decay, and other processes are random and their measurement can supply the numbers. The random.org website, for one, can supply these, and the Net::Random makes the connection for you.
rand still isn’t random, at least everyone can use the same thing without any extra work. I like it any time the
perl can bring this stuff inside to make it more portable.