Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
# -- 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found