Hi,

According to section 5.4.4 of Handbook of Applied Cryptography (by Menezes, van Oorschot and Vanstone), FIPS 140-1 specified 4 statistical tests for randomness - namely "monobit", "runs", "long run", "poker".

I implemented those tests (undocumented and not exported) in Math::GMPz.
On Windows (whether randbits == 48 or randbits == 15, when I use rand() to generate the 20000-bit sequence that these tests require, I've always found the generated bit sequence to pass those tests.
Moreover, I can see no sign of any cycling - I take a 30-bit sequence from near the end of the string, and cannot find that same sub-string elsewhere within the 20000-bit sequence.

I've also added a fifth "autocorrelation" test from the same section of the book.
Here's the script I run:
use strict; use warnings; use Math::GMPz qw(:mpz); use Test::More; my $s; # Create a string of 20000 bits for(1..20000) { $s .= int(rand(2)); } # Visually inspect the value #open (my $fh, '>', 'val.txt') or warn "Open: $!"; #print $fh $s; #close $fh; # Vectorize that string into a # Math::GMPz object my $mpz = Math::GMPz->new($s, 2); # Check that the no. of set bits is in # the range 9655..10345 cmp_ok(Math::GMPz::Rmonobit($mpz), '==', 1, 'monobit test'); # Check that the longest run of the same bit is # shorter than 34 cmp_ok(Math::GMPz::Rlong_run($mpz), '==', 1, 'long run test'); # Check that the numbers of 1-bit runs, 2-bit runs, # 3-bit runs 4-bit runs, 5-bit runs and 6-bit runs # (of both zeros and ones) meet expectations. cmp_ok(Math::GMPz::Rruns($mpz), '==', 1, 'runs test'); # Check that the no. of occurrences of the various # 4-bit sequences meets expectations. cmp_ok(Math::GMPz::Rpoker($mpz), '==', 1, 'poker test'); # Check that the number of times that # bit[pos] == bit[pos + 2] is in the # range 9655..10345. my @ret = Math::GMPz::autocorrelation($mpz, 2); cmp_ok($ret[0], '>', 9654, 'autocorrelation count > lower limit'); cmp_ok($ret[0], '<', 10346, 'autocorrelation count < upper limit'); done_testing();
Cheers,
Rob

In reply to Re: is rand random enough to simulate dice rolls? by syphilis
in thread is rand random enough to simulate dice rolls? by Discipulus

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.