in reply to Re: A little C code converted to Perl *HELP*
in thread A little C code converted to Perl *HELP*

I'd never encountered the apparant 16-bit limitation of perl's built-in rand function. most random numbers I use are pretty small. Having fallen foul of of it, I went to CPAN to look for an alternative and found Math::Random::MT which emulates (and replaces if you choose) the built-in rand and srand functions 1-for-1, and doesn't suffer this limitation. I can't vouch for the randomness of it's output (yet), but given it's authorship, it bodes well.

It uses XS, so you would need to compile it, (or download a compatible binary version AS 5.8) for your version of perl.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!

  • Comment on Re: Re: A little C code converted to Perl *HELP*

Replies are listed 'Best First'.
Re: Re: Re: A little C code converted to Perl *HELP*
by bart (Canon) on Nov 04, 2003 at 04:00 UTC
    I'd never encountered the apparant 16-bit limitation of perl's built-in rand function.
    Hmm... I always thought it was 24 bits. Maybe not for perl?

    Anyway, this old TPJ article (from 1996) discusses rand() in a little more depth, including the repeat cycle, for which slavishly following up the example implementation, in ANSI C libraries, is to blame. That was then... things might have changed since then, depending on the agenda of the P5P.

      Thanks. The article was very interesting, and produced a very useful piece of information.

      E:\perl5.8.1\bin>.\perl -MPOSIX=RAND_MAX -e"print RAND_MAX" 32767
      So, as of perl 5.8.1, rand is limited to 15-bits! S'funny how long you can go with discovering, reading about or falling foul of these things. Anyway. I have now added
      use Math::Random::MT qw[ srand rand ];

      to my .pl template file.

      This uses The Mersenne Twister PRNG, rather than a Linear Congruential one, and is good for producing the full range of 32-bit unsigned integers.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!
      Wanted!