using /dev/random

Ted Mittelstaedt tedm at
Tue Sep 23 08:10:24 UTC 2008

> -----Original Message-----
> From: owner-freebsd-questions at
> [mailto:owner-freebsd-questions at]On Behalf Of Robert Huff
> Sent: Monday, September 22, 2008 9:54 PM
> To: questions at
> Subject: using /dev/random
> 	What is the canonical way to get data from /dev/random?
> Specifically: having opened the file, how do I read the stream?
> I'm currently using
>   union {
>     float f;
>     char c[4];
>   } foo;
>   foo.f = 0.0;
>   fscanf(rand_fp,"%4c",foo.c);
> 	which doesn't seem to produce anywhere near "random bytes" as
> promised by the man page.
> 				Robert Huff

The canonical way is to use the functions random(), or srandom()
or srandomdev() or arc4random() depending on what
you need the random data for.   /dev/random is really only
useful for seeding these functions (some of them pull data
from /dev/random internally)

The thrust behind the FreeBSD /dev/random device is that
we know that getting lots of real random data from /dev/random is
difficult, however getting non-repeating seeds from
/dev/random is easy.  The device has thus been optimized
for seed generation to feed these other functions.

If you really want to roll-your-own and not use these functions
then you could read blocks from /dev/random and run
a Chi-square and Monte Carlo test on each
block and discard the ones that don't pass.

I've done my experimenting with the ENT program:


dd if=/dev/urandom bs=3000 count=100 of=random-sample

ent random-sample

Successive runs of that with different data sets and blocksizes
clearly illustrates the generator can't pass Chi-square quite
a lot of times.


More information about the freebsd-questions mailing list