in reply to PRNG/TRNG Cesaro's theorem
Of course you will want to do it differently, but here is something I wrote to play with this. I used the ntheory module because it has different RNGs as well as gcd and Pi.
#!/usr/bin/env perl use warnings; use strict; use ntheory ":all"; cesaro("drand48", sub { int(rand(1<<32)) } ); cesaro("ChaCha20", sub { irand64 } ); cesaro("/dev/urandom", sub { unpack("Q",random_bytes(8)) } ); sub cesaro { my($name, $rng) = @_; print "Using $name:\n"; for my $e (1..7) { my $n = 10**$e; my $t = 0; for (1..$n) { $t++ if gcd( $rng->(), $rng->() ) == 1; } printf "%8d %10.8f\n", $n, sqrt(6*$n/$t); } print " Pi ", Pi(), "\n\n"; }
I saved cut-n-paste by passing in the RNG code. One for Perl's default rand (which is drand48 on modern Perl), one for ntheory's CSPRNG, and one that gets data from /dev/urandom. You could replace that with something that got data from random.org. There are even a couple modules that do it automatically (Math::RandomOrg and Data::Entropy). Since we expect t/n ~ 6/Pi^2, a little algebra gives us Pi ~ sqrt(6n/t). I don't believe we can read anything into what the results might mean for the different RNG methods. The results will be different every time unless we seed (and the last can't be seeded since it is O/S collected entropy (long technical discussion of /dev/random vs. /dev/urandom vs. hardware could be had here)).
Using drand48: 10 2.73861279 100 3.06186218 1000 3.15964572 10000 3.14632429 100000 3.13988122 1000000 3.14035598 10000000 3.14143144 Pi 3.14159265358979 Using ChaCha20: 10 3.16227766 100 3.18896402 1000 3.17287158 10000 3.15675816 100000 3.14329189 1000000 3.13978836 10000000 3.14138726 Pi 3.14159265358979 Using /dev/urandom: 10 3.46410162 100 3.03821810 1000 3.11588476 10000 3.14217962 100000 3.13643021 1000000 3.14020372 10000000 3.14130744 Pi 3.14159265358979
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: PRNG/TRNG Cesaro's theorem
by marioroy (Prior) on Oct 09, 2017 at 01:30 UTC | |
by marioroy (Prior) on Oct 09, 2017 at 03:59 UTC | |
by marioroy (Prior) on Oct 09, 2017 at 06:06 UTC | |
|
Re^2: PRNG/TRNG Cesaro's theorem
by Anonymous Monk on Oct 08, 2017 at 13:45 UTC |