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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-01-2007, 10:29 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2007
Posts: 10
Rep:
|
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.
|
|
|
|
04-01-2007, 01:37 PM
|
#2
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
just use fopen() like any other file for reading.
|
|
|
|
04-01-2007, 01:38 PM
|
#3
|
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,470
Rep: 
|
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.
|
|
|
|
04-01-2007, 02:25 PM
|
#4
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,218
|
And if you are generating 10,000 you might want to look at /dev/urandom as it's non-blocking.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:59 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|