LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 11: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, 02:37 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

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

Rep: Reputation: 149Reputation: 149
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, 03:25 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,487

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
And if you are generating 10,000 you might want to look at /dev/urandom as it's non-blocking.
 
Old 04-02-2007, 02: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:08 PM
Random numbers in C loke137 Programming 9 09-11-2010 11:37 AM
random numbers deveraux83 Programming 4 05-17-2005 03:26 AM
random numbers deveraux83 Programming 2 02-28-2005 06:36 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM


All times are GMT -5. The time now is 07:35 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration