linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* /dev/random vs. /dev/urandom
@ 2005-01-07 19:05 Ron Peterson
  2005-01-07 19:16 ` Paulo Marques
                   ` (5 more replies)
  0 siblings, 6 replies; 18+ messages in thread
From: Ron Peterson @ 2005-01-07 19:05 UTC (permalink / raw)
  To: linux-kernel

When I compile and run the code below, the string of octal characters
generated by reading /dev/random contains long strings of zeroes.  I was
under the impression that /dev/random is "more random" than
/dev/urandom, and will block when it runs out of entropy until it
gathers more.  It's only when RAND_LEN is on the largish side that these
strings of zeroes appear.

I'm guessing this behaviour is to be expected; I just don't understand
it.

This is linux kernel 2.6.8.1.

#include <stdio.h>
#include <fcntl.h>
#define RAND_LEN 1024

void
read_random( const char* dev ) {
  int i, fd;
  char dat[RAND_LEN + 1];

  fd = open( dev, O_RDONLY );

  dat[RAND_LEN] = '\0';

  if( fd != -1 ) {
    read( fd, dat, RAND_LEN );
    for( i = 0; i < RAND_LEN; i++ ) {
      dat[i] = (dat[i] & 0x07) + '0';
    }
    printf( "%s: %s\n\n", dev, dat );
  } else {
    exit( 1 );
  }

  close( fd );
}

int
main( void ) {
  read_random( "/dev/random" );
  read_random( "/dev/urandom" );
  return( 0 );
}

e.g.

$ ./test

/dev/random: 5417351746543663033176215502400146103743161053514107722724572227577211175264555157343736274051424454260000000410000000000477510042000000000042400000100004776700057700000477560000103410000004100000041005777000411040100000000000000000000000000000000000000000000000000000000000000000000000000000411025574400000000000000000000000000000000000000000000100000000046404040000060574400000000010000000000000000000000001000000000000000000000000000000000006200000000000000041041770000077723004040600045770000000000000000000000006000404003400000000000000000000000000000000000000000000000000000000000000000120026302630000000000000473005300320031030000610041045104530477772004530211202304677001010000610000010000000000000001200114000100000041000000140067712002630724000004677000047300530032003103000061004104010324007777200324067710140077700101000061000001000000000000000000000000410470003104777610045100610100000000000077700000000000000006771077706104530023000300310407747770300730070000220417740776210465006504777574046504077041030000610

/dev/urandom: 1655466217206163634777531607666420217322330273327062554377500673762610503232764303737770374066306640130702327235453637234726515320440537063623075347362744022761643031224147437567521545005217571341376634432705100716667264573662535077131060611205546167254244173277374124057015170471752463673753257446324120132341703110113324160500424545414427103452004326607527142023223056654417412072453735343241747171121471174343351366215703670565624370656416102432525141711102001344506103237445351607531346546542130670340524726467315466500144520507166250124000513673351302523654724763452657772702110446526330137221714042445716025510445073154154554433625137743012150647422324104477276471266371157432250425702471733705106524743537622014253265310174041504105572260571526120773547047577667711773160042711607626651223077130635371771754041162573451444327036771052224666641252456167376416110445223405032201545552374202763513507366033462320535502163264137406437201346313404576320371006224721327520444466067703201155645714316637347641311513007645264

Happy New Year!

-- 
Ron Peterson
Network & Systems Manager
Mount Holyoke College
http://www.mtholyoke.edu/~rpeterso

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2005-01-11 14:38 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-07 19:05 /dev/random vs. /dev/urandom Ron Peterson
2005-01-07 19:16 ` Paulo Marques
2005-01-07 19:24 ` Chris Friesen
2005-01-07 19:26 ` Florian Weimer
2005-01-07 19:27 ` linux-os
2005-01-07 19:40 ` Robert Love
2005-01-07 20:50   ` Ron Peterson
2005-01-07 21:39 ` Andries Brouwer
2005-01-07 22:39   ` linux-os
2005-01-07 17:55     ` Michal Schmidt
2005-01-07 23:29     ` Andries Brouwer
2005-01-08 17:34     ` Patrick J. LoPresti
2005-01-10 12:41       ` linux-os
2005-01-10 13:03         ` Paulo Marques
2005-01-10 14:39           ` Felipe Alfaro Solana
2005-01-10 15:13         ` Patrick J. LoPresti
2005-01-10 19:24         ` David Schwartz
2005-01-11 14:38         ` Andrea Arcangeli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).