Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S261530AbVAGTGk (ORCPT ); Fri, 7 Jan 2005 14:06:40 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S261534AbVAGTF6 (ORCPT ); Fri, 7 Jan 2005 14:05:58 -0500 Received: from pagoda.mtholyoke.edu ([138.110.30.68]:60569 "EHLO pagoda.mtholyoke.edu") by vger.kernel.org with ESMTP id S261530AbVAGTFh convert rfc822-to-8bit (ORCPT ); Fri, 7 Jan 2005 14:05:37 -0500 From: Ron Peterson Date: Fri, 7 Jan 2005 14:05:36 -0500 To: linux-kernel@vger.kernel.org Subject: /dev/random vs. /dev/urandom Message-ID: <20050107190536.GA14205@mtholyoke.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8BIT Organization: Mount Holyoke College X-Operating-System: Debian GNU/Linux User-Agent: Mutt/1.5.6+20040907i Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3448 Lines: 65 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 #include #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 - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/