473,793 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

/dev/urandom vs. /dev/random

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[i] = (dat[i] >> 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: 200050617063526 437602510346655 072474031767426 306605046222320 604001730615000 243427435070303 503367456120754 173510400040000 000050000000000 007737204577300 001773560405044 500220434040000 340417736104640 464040000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000640423461 000000000000000 000000000000000 000000000000024 040000000041007 000000063461000 000000000000000 000000000000000 000000000000000 000000000000000 000000005604000 000000000340437 730400377305043 000000047730000 000000000000000 000000000300010 000000000000000 000000000000000 000000000000000 000000000000000 000000000450045 004000000002000 700413043304070 400001704340417 040504277307040 504003016045773 240400006704000 000000000000000 007004200064040 000340400006404 677300045004000 000005773200070 041304330407040 000170434046404 400047730704400 007700000777324 040000570400000 000000000000000 000000003404710 007047773220417 046704000000000 000777300000000 000000000770077 354040504160463 040704577337730 204700020004004 477357731104641 464143773650464 145773340400005 404177322046404 5704

/dev/urandom: 103137002760751 330471453227547 317447153305403 055445577241174 112554437473707 174466515247147 230165510321673 032612147044376 361336471103425 426601777564215 275375065067605 320467556775450 023436261315455 011306014006753 403112324146646 175070523075164 557244007157744 425536470274352 332415164131435 115125573101442 745565447177770 545201523517642 416662075710077 001223562356301 231171774154712 461726216545424 416106151774201 651063200446176 135471662402411 424412532245563 115451424762236 647021720005536 526122475115504 245147261210474 110355324013261 471071077552457 243201117615642 610621436346764 640170271720054 525337367306022 203674555216357 653675736157531 236151625103600 640302611364212 474263063442224 321677233017736 071340646247534 536645354770361 405340770217451 441530701353725 673225577676034 024065113165756 376675063056177 024303522225056 407015430715065 670415500366164 627720133246523 045641760343566 266442731667313 365016666076430 236732103100735 453566475641705 242516630662601 345037070127506 753760326567356 031232252607702 215722340421132 304741363321332 6054

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College
Nov 14 '05 #1
21 14195
Ron Peterson <rp******@mthol yoke.edu> scribbled the following:
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?
Your question is about the Unix devices /dev/random and /dev/urandom,
not about C. Please continue discussion about this on
comp.unix.progr ammer.
#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[i] = (dat[i] >> 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: 200050617063526 437602510346655 072474031767426 306605046222320 604001730615000 243427435070303 503367456120754 173510400040000 000050000000000 007737204577300 001773560405044 500220434040000 340417736104640 464040000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000640423461 000000000000000 000000000000000 000000000000024 040000000041007 000000063461000 000000000000000 000000000000000 000000000000000 000000000000000 000000005604000 000000000340437 730400377305043 000000047730000 000000000000000 0 000000003000100 000000000000000 000000000000000 000000000000000 000000000000000 000000004500450 040000000020007 004130433040704 000017043404170 405042773070405 040030160457732 404000067040000 000000000000000 070042000640400 003404000064046 773000450040000 000057732000700 413043304070400 001704340464044 000477307044000 077000007773240 400005704000000 000000000000000 000000034047100 070477732204170 467040000000000 007773000000000 000000007700773 540405041604630 407045773377302 047000200040044 773577311046414 641437736504641 457733404000054 0
417732204640457 04
/dev/urandom: 103137002760751 330471453227547 317447153305403 055445577241174 112554437473707 174466515247147 230165510321673 032612147044376 361336471103425 426601777564215 275375065067605 320467556775450 023436261315455 011306014006753 403112324146646 175070523075164 557244007157744 425536470274352 332415164131435 115125573101442 745565447177770 545201523517642 416662075710077 001223562356301 231171774154712 461726216545424 416106151774201 651063200446176 135471662402411 424412532245563 115451424762236 647021720005536 526122475115504

245147261210474 110355324013261 471071077552457 243201117615642 610621436346764 640170271720054 525337367306022 203674555216357 653675736157531 236151625103600 640302611364212 474263063442224 321677233017736 071340646247534 536645354770361 405340770217451 441530701353725 673225577676034 024065113165756 376675063056177 024303522225056 407015430715065 670415500366164 627720133246523 045641760343566 266442731667313 365016666076430 236732103100735 453566475641705 242516630662601 345037070127506 753760326567356 031232252607702 215722340421132 3
047413633213326 054

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Nov 14 '05 #2
Ron Peterson 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?

#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';
This assignment violates dat's bounds, invoking undefined behaviour.
So all bets are off.

Make it char dat[RAND_LEN + 1];

if( fd != -1 ) {
read( fd, dat, RAND_LEN );
for( i = 0; i < RAND_LEN; i++ ) {
dat[i] = (dat[i] >> 4 & 0x07) + 48;
Again, undefined behaviour. This time it's because you're reading
an indeterminate value, dat[i].
}
printf( "%s: %s\n\n", dev, dat );
} else {
exit( 1 );
}

close( fd );
}

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


Leaving those bugs unfixed, does swapping these two calls
reverse the behaviour? That is, if you do this instead:

read_random( "/dev/urandom" );
read_random( "/dev/random" );

are the excess 0s now associated with /dev/urandom instead?

If so, you can be reasonably sure that the problem lies with
the problems I've mentioned.
Nov 14 '05 #3
I was copying bits and pieces from other code, and made a couple of
errors. I should have typed:

char dat[RAND_LEN + 1]; (so I'm not going beyond end of array)

and

dat[i] = (dat[i] & 0x07) + '0'; (I don't need to bit shift, I just want
three bits).

Anyway, when I do these things, I still get long strings of zeroes from
/dev/random. ?

Not exactly a C question, I know. I could direct this to the kernel
list, as the kernel provides random and urandom, but I was thinking the
behaviour should be consistent between kernels, so it's not really a
kernel question. If someone can suggest a more appropriate list, that's
helpful too.

BTW, I'm using linux 2.6.x. If anyone running a BSD variant could
compile this and compare the results, that might be interesting.

-Ron-
On 7 Jan 2005 09:33:44 -0500, Ron Peterson <rp******@mthol yoke.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?

#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[i] = (dat[i] >> 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: 200050617063526 437602510346655 072474031767426 306605046222320 604001730615000 243427435070303 503367456120754 173510400040000 000050000000000 007737204577300 001773560405044 500220434040000 340417736104640 464040000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000640423461 000000000000000 000000000000000 000000000000024 040000000041007 000000063461000 000000000000000 000000000000000 000000000000000 000000000000000 000000005604000 000000000340437 730400377305043 000000047730000 000000000000000 000000000300010 000000000000000 000000000000000 000000000000000 000000000000000 000000000450045 004000000002000 700413043304070 400001704340417 040504277307040 504003016045773 240400006704000 000000000000000 007004200064040 000340400006404 677300045004000 000005773200070 041304330407040 000170434046404 400047730704400 007700000777324 040000570400000 000000000000000 000000003404710 007047773220417 046704000000000 000777300000000 000000000770077 354040504160463 040704577337730 204700020004004 477357731104641 464143773650464 145773340400005 404177322046404 5704

/dev/urandom: 103137002760751 330471453227547 317447153305403 055445577241174 112554437473707 174466515247147 230165510321673 032612147044376 361336471103425 426601777564215 275375065067605 320467556775450 023436261315455 011306014006753 403112324146646 175070523075164 557244007157744 425536470274352 332415164131435 115125573101442 745565447177770 545201523517642 416662075710077 001223562356301 231171774154712 461726216545424 416106151774201 651063200446176 135471662402411 424412532245563 115451424762236 647021720005536 526122475115504 245147261210474 110355324013261 471071077552457 243201117615642 610621436346764 640170271720054 525337367306022 203674555216357 653675736157531 236151625103600 640302611364212 474263063442224 321677233017736 071340646247534 536645354770361 405340770217451 441530701353725 673225577676034 024065113165756 376675063056177 024303522225056 407015430715065 670415500366164 627720133246523 045641760343566 266442731667313 365016666076430 236732103100735 453566475641705 242516630662601 345037070127506 753760326567356 031232252607702 215722340421132 304741363321332 6054

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College
Nov 14 '05 #4
Ron Peterson <rp******@mthol yoke.edu> wrote:
I was copying bits and pieces from other code, and made a couple of
errors. I should have typed: char dat[RAND_LEN + 1]; (so I'm not going beyond end of array) and dat[i] = (dat[i] & 0x07) + '0'; (I don't need to bit shift, I just want
three bits). Anyway, when I do these things, I still get long strings of zeroes from
/dev/random. ? Not exactly a C question, I know. I could direct this to the kernel
list, as the kernel provides random and urandom, but I was thinking the
behaviour should be consistent between kernels, so it's not really a
kernel question. If someone can suggest a more appropriate list, that's
helpful too.


Joona set the follow-up to comp.unix.progr ammer where it's more
on-topic and where I tried to answer the question...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #5
infobahn <in******@btint ernet.com> spoke thus:
if( fd != -1 ) {
read( fd, dat, RAND_LEN );
for( i = 0; i < RAND_LEN; i++ ) {
dat[i] = (dat[i] >> 4 & 0x07) + 48;
Again, undefined behaviour. This time it's because you're reading
an indeterminate value, dat[i].


It isn't necessarily undefined behavior, if RAND_LEN bytes happen to
have been read. Unless I'm missing something, the code could be
corrected with

if( fd != EOF ) { /* right? */
int bytes=read( fd, dat, RAND_LEN );
if( bytes == -1 ) {
/* error */
}
if( bytes ) { /* bytes == 0 implies end of file */
for( i=0; i < bytes; i++ ) {
/* ... */
}
}
/* ... */
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6
Ron Peterson wrote:

I was copying bits and pieces from other code, and made a couple of
errors. I should have typed:
Oh dear. We'd prefer to debug your program errors, not your
typographical errors. :-(

char dat[RAND_LEN + 1]; (so I'm not going beyond end of array)
Fair enough.

and

dat[i] = (dat[i] & 0x07) + '0'; (I don't need to bit shift, I just want
three bits).


This is still broken. Please refer to my earlier reply.
Nov 14 '05 #7
On 7 Jan 2005 09:33:44 -0500, Ron Peterson
<rp******@mthol yoke.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
Nov 14 '05 #8
Christopher Benson-Manica wrote:

infobahn <in******@btint ernet.com> spoke thus:
if( fd != -1 ) {
read( fd, dat, RAND_LEN );
for( i = 0; i < RAND_LEN; i++ ) {
dat[i] = (dat[i] >> 4 & 0x07) + 48;

Again, undefined behaviour. This time it's because you're reading
an indeterminate value, dat[i].


It isn't necessarily undefined behavior, if RAND_LEN bytes happen to
have been read.


My apologies to the OP. I didn't spot the read()! (Duh...)

In that case, I am not sure that anything remains for comp.lang.c
to contribute here. Perhaps comp.unix.progr ammer could help.
Nov 14 '05 #9
On 7 Jan 2005 11:46:47 -0500, Ron Peterson <rp******@mthol yoke.edu> wrote:
Anyway, when I do these things, I still get long strings of zeroes from
/dev/random. ?

Not exactly a C question, I know. I could direct this to the kernel
list, as the kernel provides random and urandom, but I was thinking the
behaviour should be consistent between kernels, so it's not really a
kernel question.


Pencils down. The LKML list pinned it down for me.

It was in fact a C programming error. I wasn't checking the return
value of 'read', to see how much data was actually read.

Best.

--
Ron Peterson
Network & Systems Manager
Mount Holyoke College
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3708
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are appreciated.
10
2508
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
10
5989
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
10
2514
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads me to believe that a seeded random number is still slightly random. I need a predictable random number. Here's my results Machine 1 Seed: 1453549276
2
4848
by: Grzegorz Smith | last post by:
Hi all I'm writing small python module which will be a password generator. I read that python can use system random generator on machine whit *nix os. So i start using os.urandom and when i generate random string i get something like this: urandom(8) -> '\xec2a\xe2\xe2\xeb_\n',"\x9f\\]'\xad|\xe6\xeb",'\xb0\xf8\xd3\xa0>01\xaf'. How can I convert this to hash? i change python defaultencoding from ascii to utf-8 and try convert this to...
15
2963
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a more random int ...? >>> random.randint(3, 8) 7 >>> random.randint(3, 8)
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N? ----------------------------------------------------------------------- function Random(x) { return Math.floor(x*Math.random()) } gives a random integer in the range from 0 to x-1 inclusive; use « Random(N)+1 » for 1 to N where N>2. ...
8
3915
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
6
4816
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.