R
Ron Peterson
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?
#include <stdio.h>
#include <fcntl.h>
#define RAND_LEN 1024
void
read_random( const char* dev ) {
int i, fd;
char dat[RAND_LEN];
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 = (dat >> 4 & 0x07) + 48;
}
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 );
}
506$ ./test
/dev/random: 2000506170635264376025103466550724740317674263066050462223206040017306150002434274350703035033674561207541735104000400000000500000000000077372045773000017735604050445002204340400003404177361046404640400000000000000000000000000000000000000000000000000000000000000000000000000006404234610000000000000000000000000000000000000000000240400000000410070000000634610000000000000000000000000000000000000000000000000000000000000000000000056040000000000003404377304003773050430000000477300000000000000000000000000003000100000000000000000000000000000000000000000000000000000000000000000000004500450040000000020007004130433040704000017043404170405042773070405040030160457732404000067040000000000000000000070042000640400003404000064046773000450040000000057732000700413043304070400001704340464044000477307044000077000007773240400005704000000000000000000000000000034047100070477732204170467040000000000007773000000000000000007700773540405041604630407045773377302047000200040044773577311046414641437736504641457733404000054041773220464045704
/dev/urandom: 1031370027607513304714532275473174471533054030554455772411741125544374737071744665152471472301655103216730326121470443763613364711034254266017775642152753750650676053204675567754500234362613154550113060140067534031123241466461750705230751645572440071577444255364702743523324151641314351151255731014427455654471777705452015235176424166620757100770012235623563012311717741547124617262165454244161061517742016510632004461761354716624024114244125322455631154514247622366470217200055365261224751155042451472612104741103553240132614710710775524572432011176156426106214363467646401702717200545253373673060222036745552163576536757361575312361516251036006403026113642124742630634422243216772330177360713406462475345366453547703614053407702174514415307013537256732255776760340240651131657563766750630561770243035222250564070154307150656704155003661646277201332465230456417603435662664427316673133650166660764302367321031007354535664756417052425166306626013450370701275067537603265673560312322526077022157223404211323047413633213326054
(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?
#include <stdio.h>
#include <fcntl.h>
#define RAND_LEN 1024
void
read_random( const char* dev ) {
int i, fd;
char dat[RAND_LEN];
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 = (dat >> 4 & 0x07) + 48;
}
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 );
}
506$ ./test
/dev/random: 2000506170635264376025103466550724740317674263066050462223206040017306150002434274350703035033674561207541735104000400000000500000000000077372045773000017735604050445002204340400003404177361046404640400000000000000000000000000000000000000000000000000000000000000000000000000006404234610000000000000000000000000000000000000000000240400000000410070000000634610000000000000000000000000000000000000000000000000000000000000000000000056040000000000003404377304003773050430000000477300000000000000000000000000003000100000000000000000000000000000000000000000000000000000000000000000000004500450040000000020007004130433040704000017043404170405042773070405040030160457732404000067040000000000000000000070042000640400003404000064046773000450040000000057732000700413043304070400001704340464044000477307044000077000007773240400005704000000000000000000000000000034047100070477732204170467040000000000007773000000000000000007700773540405041604630407045773377302047000200040044773577311046414641437736504641457733404000054041773220464045704
/dev/urandom: 1031370027607513304714532275473174471533054030554455772411741125544374737071744665152471472301655103216730326121470443763613364711034254266017775642152753750650676053204675567754500234362613154550113060140067534031123241466461750705230751645572440071577444255364702743523324151641314351151255731014427455654471777705452015235176424166620757100770012235623563012311717741547124617262165454244161061517742016510632004461761354716624024114244125322455631154514247622366470217200055365261224751155042451472612104741103553240132614710710775524572432011176156426106214363467646401702717200545253373673060222036745552163576536757361575312361516251036006403026113642124742630634422243216772330177360713406462475345366453547703614053407702174514415307013537256732255776760340240651131657563766750630561770243035222250564070154307150656704155003661646277201332465230456417603435662664427316673133650166660764302367321031007354535664756417052425166306626013450370701275067537603265673560312322526077022157223404211323047413633213326054