in reply to is rand random enough to simulate dice rolls?

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