Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: spew - print out random characters

by blakem (Monsignor)
on Nov 12, 2001 at 19:56 UTC ( #124849=note: print w/replies, xml ) Need Help??

in reply to spew - print out random characters

I'm not especially familiar with /dev/urandom but I understand that it can "run out" of randomness and have to wait for some more to be "collected". As such it should be used sparingly, but you're tossing out over 50% of the random chars that you fetch from it.

Since you wont be using high bit ascii, you could zero out the most significant bit, mapping it 1 to 1 with the low bit ascii. This would significantly reduce the amount of randomness that you have to throw out.

This works on my x86 linux machine.... endianness might matter, but I'm not sure.

read( R, $char, 1 ) or die "Failed to read 1 char from @{[SRC]}: $!\n" +; vec($char, 7 , 1) = 0; # map high-bit ascii to low-bit ascii
Update: As stefan k pointed out to me, /dev/urandom doesn't block when the entropy pool runs out. Instead, when no true random bits are available it will return pseudorandom bits instead. Its still probably a good idea to avoid pulling more bits than necessary though, just to keep the entropy pool happy and the pseudorandoms at bay ;-)


Replies are listed 'Best First'.
Re:x2 spew - print out random characters
by grinder (Bishop) on Nov 12, 2001 at 20:12 UTC

    OpenBSD talks about /dev/srandom, which produces a cryptographically strong source of bits, but it just blocks on my machine. I wonder if I have to fiddle with a kernel setting to make it work.

    update: turns out that on OpenBSD, you cannot just open R, '/dev/srandom' and hope that it works. Apparently (and this strikes me as incredibly klugey, but maybe it's more bulletproof), you have to do something like:

    dd if=/dev/srandom of=./bits bs=4096 count=1

    and after a suitably impressive wait you will have some strong random bits waiting for you in ./bits.

    Otherwise an alternative is to grab some bits from a random seed. Maybe for v2.0. I think Linux has /dev/random which blocks if the entropy pool is exhausted, which would be the way to go on that platform. Trouble is that OpenBSD doesn't implement that device, it is RFU.

    update: It looks like the lava lamp is dead.

    g r i n d e r

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://124849]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-10-28 09:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (260 votes). Check out past polls.