Perl 5.20 uses its own random number generator

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.

100  One Hundred Random Numbers

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 fake_random, good_enough_random, or 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 abs, chmod, or 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 rand replacements.

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.

Even though 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.

One thought on “Perl 5.20 uses its own random number generator”

  1. Windows perl random number generators don’t seem to honour the seed, only one of the four perl on win7 uses the same seed to give reproducible sequences: (good) perl-5-14-dwim (bad) cygwin64 perl-5-22, perl-5-24-sb-x64, perl-5-18-sb.

Comments are closed.