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

Anyone know if perl's built-in rand in conjunction with srand will produce consistant output across builds and platforms?.

I'm getting consistant results for the 5 versions I have on my platform:

>perl5.8.6.exe -le"srand(1); print rand for 1 .. 10" 0.001251220703125 0.563568115234375 0.19329833984375 0.8087158203125 0.584991455078125 0.4798583984375 0.35028076171875 0.89593505859375 0.82281494140625 0.74658203125

But does that extend to other platforms?


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re: rand and srand across builds and platforms?
by davis (Vicar) on Mar 08, 2005 at 15:34 UTC
    No:
    $ perl5.8.5 -le"srand(1); print rand for 1 .. 10" 0.0416303447718782 0.454492444728629 0.834817218166915 0.3359860301452 0.565489403566136 0.00176691239174431 0.18758951699996 0.990434079937664 0.750497133229519 0.366273638152734
    Update: Interestingly enough, my 32bit and 64bit HP-UX boxen produce the same output as above. Still, it's not the same as what you've got

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      This is what I get too, with perl 5.8.6.

      Thanks. I thought it unlikely, but it would have simplified some testing if it was.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
Re: rand and srand across builds and platforms?
by Fletch (Bishop) on Mar 08, 2005 at 15:35 UTC

    I'd say that looks like a no; stock 5.8.1 on OS X sez:

    0.203723445340199 0.893308938570904 0.262596539117187 0.776595210641958 0.739336229299024 0.131970010506016 0.834684628984551 0.842243446561724 0.776803253700376 0.467943108165379

    However I think Math::Random is more consistent.

      However I think Math::Random is more consistent.

      I think my favorite Math::Random::MT might be also, but it would be too much to add a prerequisite for a compiled package that's only used in testing I think.

      I can embed a few useful sequences I guess.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
Re: rand and srand across builds and platforms?
by kvale (Monsignor) on Mar 08, 2005 at 16:20 UTC
    As seen above, the rand() and srand() functions are machine dependent. If you need consistency across CPUs and C-libraries, I would recommend using a module with it's own self-contained routine. Math::Random::MT is one of the best.

    -Mark

Re: rand and srand across builds and platforms?
by JediWizard (Deacon) on Mar 08, 2005 at 15:38 UTC

    I get the same results as davis (perl 5.6.1 on Red Hat Linux 8):

    $ perl -le"srand(1); print rand for 1 .. 10" 0.0416303447718782 0.454492444728629 0.834817218166915 0.3359860301452 0.565489403566136 0.00176691239174431 0.18758951699996 0.990434079937664 0.750497133229519 0.366273638152734

    A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

    —His Holiness, The Dalai Lama

      Interesting. Maybe it comes down to the rand() function provided by the C-libraries? Would that be consistant across different GCC installs?


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.

        That sounds likely, though I can neither confirm nor deny, given that I have little to no understanding of the underlying algrothim used to generate random numbers.


        A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

        —His Holiness, The Dalai Lama

        Consistent across 5.8.0 on RH9 (glibc-2.3.2-27.9.7) and 5.6.1 on RHEL 2.1 WS (glibc-2.2.4-31.7) (i.e. I get the same sequence as above).

Re: rand and srand across builds and platforms?
by ikegami (Patriarch) on Mar 08, 2005 at 15:50 UTC

    I got the same as you on these two versions of ActivePerl:

    This is perl, v5.8.0 built for MSWin32-x86-multi-thread This is perl, v5.6.1 built for MSWin32-x86-multi-thread

      Thanks. I'd tried AS 5.6.1/5.6.2/5.8.4/5.8.5/5.8.6 and home-built versions (using the same compiler as AS) of the latter two and got consistant results.

      From the responses, it seems likely that the underlying C-library/compiler has a lot to do with the result. Either just how the rands are generated or possibly srand uses an initialisation that is platform dependant.

      It just makes testing randomly generated test sequences harder to verify than if it were consistant across builds and platforms.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
Re: rand and srand across builds and platforms?
by Anonymous Monk on Mar 08, 2005 at 17:05 UTC
    It will not. See the various Japhs floating around that (a)buse (s)rand to produce an appropriate string. They aren't cross-platform. For instance, from t/japh/abigail.t in the Perl source directory:
    srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split //=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n" SKIP: Solaris specific.
    which is skipped when running the test, because it's "Solaris specific" - or at least, it claims to be so. And the only thing that's possibly system specific in the above Japh is the use of srand/rand.
Re: rand and srand across builds and platforms?
by esskar (Deacon) on Mar 08, 2005 at 17:13 UTC
    It's probably a problem of running on a POSIX system or on ne not POSIX system!

      Is OSX POSIX(-complient?)


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.

        OS X is basically Mach with a FreeBSD userland, so pretty much so yes.