# -- 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.