Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

max random

by wolverina (Beadle)
on Sep 26, 2005 at 23:44 UTC ( [id://495233]=perlquestion: print w/replies, xml ) Need Help??

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

just wondering... what is the highest number perl's random variable can handle? thanx... Lisa

Replies are listed 'Best First'.
Re: max random
by blokhead (Monsignor) on Sep 27, 2005 at 00:16 UTC
    As far as I know, most perls use drand48, which is a 48-bit PRNG. You can definitively check how many random bits your perl's PRNG has using the Config module:
    perl -MConfig -le 'print $Config{randbits}'

    Update: The clever PodMaster suggests perl -V:randbits as another way to access the same information.

    blokhead

Re: max random
by tomazos (Deacon) on Sep 27, 2005 at 08:24 UTC
    On my WinXP machine it is 2 to the power of 15.

    On my Slackware linux machine it is 2 to the power of 48.

    On your machine it is 2 to the power of "perl -V:randbits" as mentioned previously.

    -Andrew.


    Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
Re: max random
by blazar (Canon) on Sep 27, 2005 at 09:56 UTC
    just wondering... what is the highest number perl's random variable can handle? thanx... Lisa
    Just a minor nitpick: rather than to a hypothetical "perl's random variable" you're really asking about the bit depth of rand, aren't you? Them, as many people already told you, it should be 48.

    But beware! it depends on a compiler option. The (easiest) way to find it out, not mentioned yet, is:

    perl -V:randbits

    In particular under all Linux platforms I've had access to, indeed it's 48. But for some reason AS's popular ActivePerl distro has it set to 15!!

    Pay attention to this circumstance: I've been bit in the neck myself, as you can see at these clpmisc threads: Re: Fast random string generation, and the subsequent Linux vs. Windows: different behaviour..., also available from Google Groups respectively here and here.

    Basically I was suggesting a fast way to generate a 20_000 chars long random string by means of generating them 4 at a time, but it happened to "fail" (some of them were constantly nulls) under Windows.

      Not a compiler option, and it's not "set" to 15. randbits is supposed to show how many bits the c library function used to generate random numbers actually provides. So on WinXP using rand from the msvcrt library, you only get 15 (my recollection was that it was actually 14) bits. Note that this isn't a limitation of Windows; if someone were to go to the trouble of bundling a random number generator with perl, there'd be no need to depend on whatever the c library dishes up.
        Not a compiler option, and it's not "set" to 15. randbits is supposed to show how many bits the c library function used to generate random numbers actually provides. So on WinXP using rand from the msvcrt library, you only get 15 (my recollection was that it was actually 14) bits. Note that this isn't a limitation of Windows;
        I stand corrected. Thank you for the clarification. I still think the central concept of my reply does apply. Indeed I had submitted a bug report to AS and never got a reply: always wondered why since it seems so simple to me; now much less so.
        if someone were to go to the trouble of bundling a random number generator with perl, there'd be no need to depend on whatever the c library dishes up.
        Also, some people have {considered,asked} before (although I can't find fresh references offhand) doing so in order to increase rand's quality too, which is well known not to be particularly good, which is why modules like Math::TrulyRandom exist.

      Well, to be even more nitpicky, is the question the "highest number" (i.e. largest number that can be represented in perl), or the "period" of the pseudo-random number generator (i.e. the number of random numbers that can be generated before the cycle repeats).

      Generally, the second is the more interesting value. And it's certainly possible to get higher periodicities in Perl than what comes with the built-in rand. Based on its release activity and documentation, I'd recommend anyone needing high-quality pseudo-randoms look at Math::Random::MT::Auto, which can be used as a drop-in replacement for rand.

      use Math::Random::MT::Auto 'rand';

      It offers a periodicity of 2^19937 - 1 using the Mersenne twister algorithm. Among other useful features is the irand function to directly generate random integers. So if your Perl is compiled for 64-bit integers, you can generate 64-bit pseudo-random numbers.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: max random
by gargle (Chaplain) on Sep 27, 2005 at 06:39 UTC

    Hi,

    Silly me, but, because of a brain f*rt, I had to look up PRNG on wikipedia.

    It seems I'm having more and more trouble remembering all those tla's.

    --
    if ( 1 ) { $postman->ring() for (1..2); }
      perl -le 'print length("PRNG")'
      4

      FOUR letter acronym?

        My mistake, I should have linked to the jargon file, especially entry 2. The jargon file states that a TLA can have more than 3 letters!

        --
        if ( 1 ) { $postman->ring() for (1..2); }
        TLA: Three Letter Acronym
        ETLA: Extended Three Letter Acronym
        DETLA: Double Extended Three Letter Acronym

        'TLA' is a TLA, 'ETLA' is a ETLA and 'DETLA' is a DETLA.

        AM

Re: max random
by scooper (Novice) on Sep 29, 2005 at 07:39 UTC
    Also, Perl's random numbers can handle all probabilities between a low value of 0 and a high value of 1.

      You have to be careful with this. If you require random integers in a range greater than the number of random bits provided by your Perl installation, and get them using the usual multiply and int idiom, you won't get the results you want.

      Eg. using the 15-bit rand() available from AS Perl to produce integers in the range 0 .. 100,000:

      $x{ int( rand 100000 ) }++ for 1 .. 1000000;; print "$_ => $x{ $_ }" for sort{ $a <=> $b } keys %x;; 0 => 45 3 => 30 6 => 31 9 => 32 12 => 30 15 => 27 18 => 24 21 => 20 24 => 22 27 => 23 30 => 36 ...

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://495233]
Approved by ikegami
Front-paged by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found