473,804 Members | 3,548 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
21 14197
Ron Peterson <rp******@mthol yoke.edu> spoke thus:
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.


Not to boast, but note my response elsethread :)

--
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 #11

In article <41********@nap .mtholyoke.edu> , Ron Peterson <rp******@mthol yoke.edu> writes:

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.


This is a POSIX programming error, not a C programming error. You
could make precisely the same mistake with any language capable of
making system calls on a POSIX system. read is not a C function;
it's a POSIX function.

And that is precisely why this question was OT for comp.lang.c, and
topical for comp.unix.progr ammer.

--
Michael Wojcik mi************@ microfocus.com

Painful lark, labouring to rise!
The solemn mallet says:
In the grave's slot
he lies. We rot. -- Basil Bunting
Nov 14 '05 #12

In article <cr**********@c hessie.cirr.com >, Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:

if( fd != EOF ) { /* right? */
Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.

fd is the return value from the POSIX open function. open returns
a file descriptor, which is a non-negative integer, or -1 on failure.
Since EOF may not be -1, that's not the appropriate value to compare
it to.
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++ ) {
/* ... */
}
}


This does fix part of the error in the OP's code (it appears that
he expects the read_random function to populate the entire buffer,
so it really needs to loop around calls to read until it's done so),
but note that the test "if( bytes )" is extraneous, since the for
loop does nothing if bytes == 0.

--
Michael Wojcik mi************@ microfocus.com

Not the author (with K.Ravichandran and T.Rick Fletcher) of "Mode specific
chemistry of HS + N{_2}O(n,1,0) using stimulated Raman excitation".
Nov 14 '05 #13
Michael Wojcik <mw*****@newsgu y.com> spoke thus:
Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.


Well, it wasn't totally off-topic, since we agree that OP failed to
use the standard read() function correctly.

--
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 #14
Christopher Benson-Manica wrote:
Michael Wojcik <mw*****@newsgu y.com> spoke thus:

Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.

Well, it wasn't totally off-topic, since we agree that OP failed to
use the standard read() function correctly.


There is no standard function in C called "read". If you are referring
to the POSIX function called "read", then you know that it is topical in
newsgroups addressing POSIX issues and off-topic in newsgroups
addressing C issues. Your "reason" for the question's not being
"totally off-topic" is completely bogus.
Nov 14 '05 #15
Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:
Michael Wojcik <mw*****@newsgu y.com> spoke thus:
Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.


Well, it wasn't totally off-topic, since we agree that OP failed to
use the standard read() function correctly.


What standard read() function would that be? read() is POSIX, not
ISO C. fread() is ISO C.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16
Christopher Benson-Manica wrote:

Michael Wojcik <mw*****@newsgu y.com> spoke thus:
Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.


Well, it wasn't totally off-topic, since we agree that OP failed to
use the standard read() function correctly.


No, that's not why it wasn't off-topic (for reasons which have been
explained already). It wasn't off-topic because it was a question
about C. The fact that the OP used read() turned out to be relevant,
at which point it would have been perfectly reasonable to redirect
him to another newsgroup if it weren't for the fact that his
question had by that stage been answered already.

We cannot expect everyone asking questions here to know everything
about C, and particularly what is and what is not C. That does not
mean that all questions are topical here if they contain the letter
C! A detailed explanation of the read() function and its part in
the OP's downfall would have been off-topic here, but the question
itself was fine. Consider this question:

Q: As my example code shows, I'm struggling with fork(). Could
someone tell me what I'm doing wrong?

which is a perfectly sensible question to ask in this newsgroup.
Of course, the only topical /answer/ would be to refer the OP to a
*nix group, but that's beside the point.
Nov 14 '05 #17

In article <41************ ***@btinternet. com>, infobahn <in******@btint ernet.com> writes:
Christopher Benson-Manica wrote:
Michael Wojcik <mw*****@newsgu y.com> spoke thus:
Wrong, I'm afraid, and yet another example of why we shouldn't try
to answer off-topic questions here.
Well, it wasn't totally off-topic, since we agree that OP failed to
use the standard read() function correctly.


No, that's not why it wasn't off-topic (for reasons which have been
explained already). It wasn't off-topic because it was a question
about C. The fact that the OP used read() turned out to be relevant,
at which point it would have been perfectly reasonable to redirect
him to another newsgroup if it weren't for the fact that his
question had by that stage been answered already.


Agreed, but note that by "answer off-topic questions" above I meant
specifically "try to provide a direct answer, rather than a
redirection to an appropriate group" (usage which is frequently
employed in topicality discussions here).

Of course when confronted with questions that the questioner believes
are about C, but actually turn on an implementation dependency, we
should "answer" them in the sense of providing a redirection. What
we should avoid doing is trying to answer the substance of the
question, for the reasons commonly cited: we may be mistaken about
the implementation, and our responses will not be vetted by the
community dedicated to that implementation.
A detailed explanation of the read() function and its part in
the OP's downfall would have been off-topic here, but the question
itself was fine.


That was indeed my point (except that I referred to Christopher's
comment regarding the return value of the open() function, not the
read() function).

--
Michael Wojcik mi************@ microfocus.com

Auden often writes like Disney. Like Disney, he knows the shape of beasts --
(& incidently he, too, might have a company of artists producing his lines) --
unlike Lawrence, he does not know what shapes or motivates these beasts.
-- Dylan Thomas
Nov 14 '05 #18
Keith Thompson <ks***@mib.or g> spoke thus:
What standard read() function would that be?


The one that I thought existed after looking it up in K&R; I should
have understood the difference between "library function" (standard)
and "system call" (not standard). Sorry.

--
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 #19
Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:
Keith Thompson <ks***@mib.or g> spoke thus:
What standard read() function would that be?


The one that I thought existed after looking it up in K&R; I should
have understood the difference between "library function" (standard)
and "system call" (not standard). Sorry.


The distinction between library functions and system calls isn't the
same as the distinction between standard and non-standard functions.
System calls are an implementation detail; a function in the C
standard library could easily be implemented as a system call. (The
time() function typically is, for example.) Similarly, there are
plenty of functions defined by POSIX but not by ISO C that are
typically implemented as library functions rather than as system
calls.

<OT>
A system call generally works by invoking code within the OS kernel
rather than in a user-mode library. On a Unix-like system, system
calls are documented by man pages in section 2, library functions in
section 3.
</OT>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #20

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

Similar topics

28
3711
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
2509
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
5991
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
4849
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
2965
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
3918
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
10571
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...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.