tim.qfs has asked for the wisdom of the Perl Monks concerning the following question:
sub TRNGrand { (my $range) = @_; #This Perl subroutine almost instantly outputs a true random #number between zero and $range inclusive to 15 decimal places. # #If you are considering purchasing a hardware TRNG, save your #money and use this script instead. # #The author has only tested this subroutine on the Windows OS #and therefore does not know if it works on Unix. # #Unfortunately, the Perl "bignum" module and associated modules #can break this subroutine so if you are writing a Perl script #which uses these modules, please put the "use bignum;" command #and associated commands somewhere after the text of this #subroutine. use Time::HiRes qw(gettimeofday); my $decimalplace; my $bitstring; my $binaryplace; my @loopcount; my $loopcountsgroupA; my $loopcountsgroupB; my $runnumber; my $x8microsecondschanges; my $previousx8microseconds; my $seconds; my $microseconds; my $x8microseconds; my $bit; my $bitstring; my $decimaldigit; my $decimaldigits; my $decimalplace; $decimalplace = 1; until ($decimalplace == 15) { $bitstring = ""; for ($binaryplace = 0; $binaryplace <= 3; $binaryplace++) { @loopcount = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); $loopcountsgroupA = 0; $loopcountsgroupB = 0; until ($loopcountsgroupA ne $loopcountsgroupB) { for ($runnumber = 0; $runnumber <= 32; $runnumber++) { $x8microsecondschanges = 0; $previousx8microseconds = "null"; until ($x8microsecondschanges == 2) { ($seconds,$microseconds) = gettimeofday; $x8microseconds = int($microseconds / 8); if($previousx8microseconds ne "null" && $x8microseconds ne $previousx8microseconds) { $x8microsecondschanges++; } $previousx8microseconds = $x8microseconds; if($x8microsecondschanges == 1) {$loopcount[$runnumber]++;} } } $loopcountsgroupA = $loopcount[1]+$loopcount[3]+$loopcount[6]+$loopcount[8]+ $loopcount[9]+$loopcount[11]+$loopcount[14]+$loopcount[16]+ $loopcount[18]+$loopcount[20]+$loopcount[21]+$loopcount[23]+ $loopcount[26]+$loopcount[28]+$loopcount[29]+$loopcount[31]; $loopcountsgroupB = $loopcount[2]+$loopcount[4]+$loopcount[5]+$loopcount[7]+ $loopcount[10]+$loopcount[12]+$loopcount[13]+$loopcount[15]+ $loopcount[17]+$loopcount[19]+$loopcount[22]+$loopcount[24]+ $loopcount[25]+$loopcount[27]+$loopcount[30]+$loopcount[32]; } if($loopcountsgroupA < $loopcountsgroupB) {$bit = 0;} else {$bit = 1;} $bitstring = "$bitstring$bit"; } $decimaldigit = unpack("N",pack("B32",substr("0"x32 .$bitstring,-32))); if($decimaldigit < 10) { $decimaldigits = "$decimaldigits$decimaldigit"; $decimalplace++; } } if($range eq "") {$range = 1;} return "0.$decimaldigits" * $range; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New software TRNG
by Corion (Patriarch) on Jan 29, 2014 at 15:00 UTC | |
|
Re: New software TRNG
by hdb (Monsignor) on Jan 29, 2014 at 15:01 UTC | |
by tim.qfs (Novice) on Jan 29, 2014 at 16:13 UTC | |
by AnomalousMonk (Archbishop) on Jan 29, 2014 at 20:25 UTC | |
by hdb (Monsignor) on Jan 29, 2014 at 20:35 UTC | |
by tim.qfs (Novice) on Jan 29, 2014 at 22:08 UTC | |
by hdb (Monsignor) on Jan 31, 2014 at 07:57 UTC | |
by AnomalousMonk (Archbishop) on Jan 30, 2014 at 00:21 UTC | |
| |
|
Re: New software TRNG
by LanX (Saint) on Jan 29, 2014 at 15:11 UTC | |
|
Re: New software TRNG
by GrandFather (Saint) on Jan 29, 2014 at 22:36 UTC | |
by tim.qfs (Novice) on Jan 29, 2014 at 23:01 UTC | |
by GrandFather (Saint) on Jan 30, 2014 at 01:14 UTC | |
by tye (Sage) on Jan 30, 2014 at 02:39 UTC | |
by tim.qfs (Novice) on Jan 30, 2014 at 01:41 UTC | |
by GrandFather (Saint) on Jan 30, 2014 at 02:54 UTC | |
|