My task was to generate random 64 character hex strings to feed to an external program. I started by calling rand() for each character but found that pretty slow, so I eventually wound up with this much faster version that derives extra data from each random seed:
# open my $pipe, '|-', 'pv -perlt | external-command' or die $!; my $pipe = *STDOUT{IO}; # https://stackoverflow.com/questions/63172427/why-does-perl-qx-hang-i +n-mojoliciouslite-but-not-in-an-ordinary-program/63178400#63178400 my $qfn = '/dev/urandom'; open my $fh, '<:raw', $qfn or die "$qfn: $!"; while (1) { my ($num, $buf) = (32, ''); while ($num) { my $rv = sysread($fh, $buf, $num, length $buf) // die "$qfn: $ +!"; die "$qfn: Premature EOF\n" unless $rv; $num -= $rv; } # For each chunk of random data, derive 1023 extra chunks. This re +duces # entropy consumption and increases throughput. # # Negate bits for my $bytes ($buf, ~$buf) { # Reverse bits for my $bits ($bytes, pack('b*', unpack 'B*', $bytes)) { # Reverse nibbles for my $hex (unpack("H*", $bits), unpack("h*", $bits)) { # Reverse hex string for my $str ($hex, scalar reverse $hex) { say {$pipe} $str; # Rotate hex string say {$pipe} substr($str, $_), substr($str, 0, $_) for 1 .. 63; } } } } } close $pipe;

In reply to Increasing throughput of random data by Anonymous Monk

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.