So each has a fixed number of possible values and it will simply cycle through those in the exact same order each time if you actually call it 2**$bits times. The number of effectively different seed values is called the "period".

Sorry tye, but you are very wrong on this, and I can prove it.

The "period" is the number of values you have to draw before the entire sequence of 2**n-bits values, repeat in the same sequence, in their entirety!

The Mersenne Twister MT19937, with 32-bit word length, has a period of 219937 - 1. How could that be if your statement above was true?

Of course, that is not a LCPRNG.

But, the rand() provided by MSVC is a linear congruential PRNG. And it only produce 15-bits on entropy. Proof:

C:\test>perl -E"++$h{ int( rand 65536 ) } for 1 .. 1e6; say scalar key +s %h; grep{ $_ & 1 } keys %h or say 'No odd numbers found'" 32768 No odd numbers found

And for the whole sequence to repeat, the first two values would have to repeat one after the other first. And according to you, that should happen within the first 32768 values produced.

It doesn't. (With srand(1)):

C:\test>randperiod -M=2 41 18467 i:1 n:412286284 First sequence of 2 values repeated itself after 412286284 calls to ra +nd

That's 412 million values generated before the first pair are repeated in sequence.

Now let's try to match the first 3 values produced:

C:\test>randperiod -M=3 41 18467 6334 i:2 n:2147418117 First sequence of 3 values repeated itself after 2147418117 calls to r +and

That's 2 billion values drawn before the first 3 values repeat in sequence.

The test code:

#! perl -slw use strict; sub rand32768{ int( rand 32768 ) } $|++; our $M //= 10; srand( 1 ); my @first = map rand32768(), 1 .. $M; print "@first"; my $n = $M; OUTER: while( 1 ) { ++$n until rand32768 == $first[ 0 ]; for my $i ( 1 .. $M - 1 ) { ++$n; printf "\ri:$i n:$n"; redo OUTER unless rand32768() == $first[ $i ]; } last; } print "\nFirst sequence of $M values repeated itself after $n calls to + rand";

I've tweaked the code to log how many value were drawn for each increasing length sequence. I'll leave it running over night.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^4: How likely is rand() to repeat? (srand) by BrowserUk
in thread How likely is rand() to repeat? by desertrat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.