Re: /dev/urandom vs. /dev/random

From: Chris Croughton (chris_at_keristor.net)
Date: 01/07/05


Date: Fri, 7 Jan 2005 17:45:22 +0000

On 7 Jan 2005 09:33:44 -0500, Ron Peterson
   <rpeterso@mtholyoke.edu> wrote:

> In the following piece of code, which simply generates a sequence of
> (random) octal codes, I'm surprised by the apparent non-randomness of
> /dev/random. It's not noticeable unless RAND_LENGTH is largish. I was
> under the assumption that /dev/random was "more random" than
> /dev/urandom, and that it would block if it ran out of entropy until it
> got more. Why am I seeing so many zeroes in my output?

For a start, your question is not really C related, it could happen in
any language. Not only is /dev/random device specific but you are also
using POSIX I/O functions (open/read etc.) rather than the C ones.

> read( fd, dat, RAND_LEN );

What is the length read? It is quite possible that read() is returning
when it gets the first byte or few bytes, and the rest of the array is
undefined.

> for( i = 0; i < RAND_LEN; i++ ) {

You are assuming that you know that the whole array was filled. I
suggest that it wasn't.

(I have a reason for suggesting this. I used dd(1) to read a block from
/dev/random into a file. The first time it got 1024 bytes. The next
time it got two, the one after around 30. I'd be pretty sure that dd(1)
is using the same mechanism to read data. That was on Linux kernel
2.2.19...)

Which does make it a sort-of C question, because if you used the C I/O
(fopen, fread etc.) you might find different behaviour. Or perhaps
not...

Chris C



Relevant Pages

  • Re: Keeping score
    ... Sorry for misunderstanding the first time. ... you are adding one to numCorrect or numIncorrect no matter ... I have tried using the qAnswered array found on the site, ... Dim visitedAs Boolean ...
    (microsoft.public.powerpoint)
  • Strange "Stack Overflow" message occurring with use of vector
    ... this is the first time I've posted to it, ... with a dynamic array). ... binary chains stored consecutively in the vector at each iteration), ... the code breaks with a "Stack Overflow" error message. ...
    (microsoft.public.vc.language)
  • Re: PHP Search & Links
    ... >> file as an array. ... >and to tell you the truth it is the first time i see something like ... I have replaced the $ at times or the echo I ... the part displaying the lines should display all lines and ...
    (alt.php)
  • Re: Temperature Script
    ... > you want to take first time for day, ... > just a tip, when posting a question, it is great that you show code and ... > I need two items in this array like this ...
    (alt.php)