Category: | cryptography |
Author/Contact Info | bitshiftleft |
Description: | With better random number generators in the unix environments (/dev/random). I was looking for something like it on Win32, and there is. My motive is that I think some games that base there learning on neural nets may be learning on the poor periodic rand() type of random number generation. This means that the Neural Net may be learning the next roll of the dice rather than the game strategy itself. The Perl rand() repeats every 32000 times. Cryptographically generated random numbers gather entropy to seed there generators. |
# -- csrand.pl - cryptographically secure random number generator # -- For some reason the CAPICOM_ENCODE_BINARY option doesn't work in +the Activestate Win32 perl 5.8 implementation # -- so I used CAPICOM_ENCODE_BASE64 and decoded that for the random n +umber. use Win32::OLE; Win32::OLE->Option(Warn => 3); # get CAPICOM VarType CONSTANTS from Capicom.dll, commented out - tak +es too long searching registry sometimes #use Win32::OLE::Const 'CAPICOM v2.1 Type Library'; # --- use the constants below instead , thats all we need - get values + from object browser $CAPICOM_ENCODE_ANY = -1; $CAPICOM_ENCODE_BASE64 = 0; $CAPICOM_ENCODE_BINARY = 1; my $RNG = Win32::OLE->new("CAPICOM.Utilities"); # download Capicom.dl +l from Microsoft.com my $die1 = 255; my $die2 = 255; #--- for uniform distribution of die faces: 6 divides 252 252 = 251+1 +(includes zero) while ($die1 > 251){ # reject if true - get another random number, mus +t be divisible by 6 for uniformity $die1 = $RNG->GetRandom({Length => 1 , EncodingType => $CAPICOM_ENCOD +E_BASE64}); # get one byte of random bits $die1 = unpack "C", DECODEBASE64($die1); } while ($die2 > 251){ # reject if true - get another random number, mus +t be divisible by 6 for uniformity $die2 = $RNG->GetRandom({Length => 1 , EncodingType => $CAPICOM_ENCOD +E_BASE64}); # get one byte of random bits $die2 = unpack "C", DECODEBASE64("$die2"); } # --- the usual modulo 6 + 1 on an integer print "You rolled ",$die1 % 6 + 1, ",",$die2 % 6 +1 ,"\n"; sub DECODEBASE64{ # see Programming Perl 2nd edition my $die = $_[0]; $die =~ tr|A-Za-z0-9+/||cd; $die =~ tr|A-Za-z0-9+/| -_|; $die = unpack("u",pack("c", 32+(length $die)* .75) . $die); return $die; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A better rand() for Win32
by BrowserUk (Patriarch) on Jul 30, 2007 at 18:24 UTC | |
by Anonymous Monk on Jul 30, 2007 at 21:06 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 00:40 UTC | |
by ysth (Canon) on Jul 30, 2007 at 21:08 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 00:38 UTC | |
by pKai (Priest) on Jul 31, 2007 at 12:26 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 17:50 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 12:33 UTC | |
by ysth (Canon) on Jul 31, 2007 at 01:01 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 01:22 UTC | |
by bart (Canon) on Jul 31, 2007 at 20:44 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 21:15 UTC | |
by bart (Canon) on Jul 31, 2007 at 21:19 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 21:49 UTC | |
| |
Re: A better rand() for Win32
by pmonk4ever (Friar) on Sep 22, 2007 at 01:58 UTC | |
by bitshiftleft (Sexton) on Oct 04, 2007 at 20:32 UTC | |
by pKai (Priest) on Sep 22, 2007 at 10:20 UTC | |
by pmonk4ever (Friar) on Oct 01, 2007 at 23:05 UTC |
Back to
Code Catacombs