in reply to Re: Rand Wackiness!
in thread Rand Wackiness!

Just out of curiosity, do you know where/why this happens?

My understanding of BigInt routines is limited, but it is literally doing something with 32768 (aka 2**15) that makes this happen?

thanks
jason.

Replies are listed 'Best First'.
Re^3: Rand Wackiness!
by BrowserUk (Patriarch) on May 11, 2006 at 21:46 UTC

    The rand function (on Win32) only produces values in the range 0 .. 32767*.

    If you multiply that by any factor, you will get multiples of the base values. E.g.

    #! perl -slw use strict; use Data::Dumper; my %hash; $hash{ int rand() * 65536 }++ for 1 .. 1e6; print "$_ => $hash{ $_ }" for sort{ $a <=> $b } keys %hash; __END__ 0 => 26 2 => 30 4 => 26 6 => 32 8 => 33 10 => 23 ... 65520 => 33 65522 => 28 65524 => 26 65526 => 24 65528 => 23 65530 => 26 65532 => 43 65534 => 26

    Note how there are no odd values produced. The multiplier is a twice the base, so all you get are multiples of two.

    If the multiplier is 3* the base, you only get powers of 3. And so on.

    For non-exact multipliers, the pattern is less obvious, but there will still be (lots) of values that will never be produced.

    Math::Random::MT has a much larger base, a huge periodicity, it's written in XS for reasonable speed and is available as a PPM from AS. Personally, I think it should have been included in Perl to replace CRT PRNGs years ago.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Rand Wackiness!
by swampyankee (Parson) on May 11, 2006 at 22:12 UTC

    To quote the documentation for the rand function:

    (Note: If your rand function consistently returns numbers that are too large or too small, then your version of Perl was probably compiled with the wrong number of RANDBITS.)

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele