# -- 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; }
In reply to A better rand() for Win32 by bitshiftleft
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |