LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2007, 10:29 AM   #1
guguma
LQ Newbie
 
Registered: Feb 2007
Posts: 10

Rep: Reputation: 0
Question using /dev/random to output random numbers on a text file


Hello and good days,

First of all this is not a programming class homework or whatever so please do not humiliate me with that kind of approach.

I have searced around and I found that /dev/random can create an "entropy pool" from keyboard, mouse, or CPU interrupts and then these random interrupts can be used to create a set of true random numbers.

I need to create a set of random numbers (about 10.000) between 0 and 1. And I really need them to be uniformly distributed, then I saw all the things about /dev/random and hoped that I can use it to do this. I can code in C language but I am not that much of an expert so I have no idea how to access this /dev/random to accomplish what I have hoped.

So could anyone please help me on this, any kind of material, link (maybe around some dark corners of te web there is a page that slipped my attention) or knowledge is appreciated.

Thanks in advance.
 
Old 04-01-2007, 01:37 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
just use fopen() like any other file for reading.
 
Old 04-01-2007, 01:38 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The numbers between 0 and 1 are not integers. That makes the whole problem non-trivial. The first issue is what format you want them to have. In standard C you have float and double, but you probably already know that you can't get all possible values using float or double type. So first think about the precision you need.

Then you just need to get a number of bits from /dev/random and scale them to what you want. For instance, if you want a value between 0.00 and 1.00 (with two digits) you can get one byte and convert the 256 possible values to 99 you want. You may make the scale linear to make things simplier, so for instance (the issue is that 256 divided by 99 is not an integer) 0x02 will be 0.01 and 0xfe - 0.99. You can also write a table with ranges to make things simplier.
 
Old 04-01-2007, 02:25 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
And if you are generating 10,000 you might want to look at /dev/urandom as it's non-blocking.
 
Old 04-02-2007, 01:42 PM   #5
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
Though a bit more complicated, I think it is usually regarded as a better idea to seed a prng from /dev/rand and then use that to generate a series of random numbers (as opposed to pulling them from /dev/rand). One option that should provide random, uniformly distributed, etc, etc, etc.. numbers is the Mersenne twister:
http://en.wikipedia.org/wiki/Mersenne_twister

You can write your own implimentation, or use one of the existing ones:
http://www.gnu.org/software/gsl/
http://www.cs.hmc.edu/~geoff/mtwist.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
generating random numbers in C abk4523 Programming 20 01-10-2011 04:08 PM
Random numbers in C loke137 Programming 9 09-11-2010 10:37 AM
random numbers deveraux83 Programming 4 05-17-2005 02:26 AM
random numbers deveraux83 Programming 2 02-28-2005 05:36 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration