Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i tested a program under 5.42 and 5.38 and it worked the same on both versions. on how many computers would this program produce the expected output?

/usr/bin/perl -v This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-li +nux-gnu-thread-multi /usr/bin/perl -E 'srand(80085);say(join("",map({g($_)}("3f5a6471135061 +5c5b4f5867114c5666521f59535b604e57"=~m/../g))));sub g($num){chr(hex($ +num)+int(rand(31)));}' Just another Perl hacker
perl -v This is perl 5, version 42, subversion 0 (v5.42.0) built for x86_64-li +nux perl -E 'srand(80085);say(join("",map({g($_)}("3f5a64711350615c5b4f586 +7114c5666521f59535b604e57"=~m/../g))));sub g($num){chr(hex($num)+int( +rand(31)));}' Just another Perl hacker

Replies are listed 'Best First'.
Re: how portable is the random number generator?
by haukex (Archbishop) on Jan 11, 2026 at 08:27 UTC

    Why? It's a random number generator.

    If this is for testing, then mock the rand function (or wrap it in a function for easier mocking).

    If you need a reliable sequence of pseudo-random numbers for whatever reason, then use a fixed PRNG implementation (search CPAN for PRNG and Math::Random). There are even ones simple enough that you can implement them yourself, like say Xorshift (though there's a module on CPAN for that too, of course).

    If it's just for a JAPH, then the perldelta for 5.20 says "Perl now uses its own internal drand48() implementation on all platforms" (also mentioned in the rand doc), with no mention of the PRNG in the deltas since then - but no guarantees that this won't change in the future, since again, it's a random number generator.

    But at least that answers your question: Your JAPH should produce the same output on Perl 5.20 though at least the current 5.42 on all platforms.

      no guarantees that this won't change in the future

      Indeed, just yesterday on P5P: Should we upgrade to a new PRNG in core?

      And a relevant note from that thread by Russ Allbery:

      The most secure option is to not use any type of pseudo-random number generator and instead rely on /dev/random. If your concern is security, you should not use rand and you should not use any other new algorithm that similarly does not use /dev/random. ... If /dev/random is available and you want random numbers for security purposes, you should just use it via Crypt::URandom, Crypt::Random, etc.
Re: how portable is the random number generator?
by afoken (Chancellor) on Jan 13, 2026 at 16:52 UTC

    https://xkcd.com/221/

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      xkcd.com/1277
Re: how portable is the random number generator?
by ikegami (Patriarch) on Jan 13, 2026 at 01:46 UTC

    I believe it is or was configurable at build-time.

    There was some wild difference between OSes.

    This means you shouldn't rely on consistency between builds.

Re: how portable is the random number generator?
by sectokia (Friar) on Jan 25, 2026 at 22:30 UTC

    Perl rand() uses C drand48() or if that isn't available C rand().

    In the definition of both of these the algorithm is not defined, only the interface is.

    A few good fast generator that can repeat exact sequences given big seeds is the Mersenne Twister.

      As was already mentioned, this is no longer true. Perl has used its own implementation of drand48 since 5.20.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: how portable is the random number generator?
by harangzsolt33 (Deacon) on Jan 13, 2026 at 02:39 UTC
    If you need consistency in your random numbers, you could use your own random number generator. For example:

    #!/usr/bin/perl -w use strict; use warnings; my $S = 7379.74575; # Seed my $A = 171.379999; # Multiplier my $B = 556.625571; # Offset my $C = 3799.197357; # Mod # Generate random numbers between $MIN and $MAX: my $MIN = 0; my $MAX = 80; my $myRandomInt; for (my $i = 0; $i < 130; $i++) { $S = FMOD($S * $A + $B, $C); $myRandomInt = int($S % ($MAX - $MIN) + $MIN); print $myRandomInt, "\t"; } exit; # Usage: NUMBER = FMOD(DIVIDEND, DIVISOR) sub FMOD { return $_[0] - int($_[0] / $_[1]) * $_[1]; }

    This program should generate the following list regardless of the type of computer or OS or Perl version:

    4 32 79 6 8 27 42 29 79 + 76 65 45 37 8 28 54 2 77 55 + 66 51 78 47 74 11 8 70 46 59 + 66 18 71 50 34 54 17 68 12 41 + 72 4 66 16 72 68 75 13 69 26 + 77 25 36 3 11 38 72 75 41 70 + 15 57 17 40 77 31 39 3 56 48 + 23 60 79 33 57 51 19 4 74 41 + 64 53 22 41 49 19 54 52 63 49 + 76 65 9 61 21 35 25 67 12 30 + 3 22 54 50 44 34 63 56 60 66 + 57 77 52 56 41 37 24 73 26 58 + 71 57 20 1 14 6 21 62 30 63 + 0