using /dev/random to output random numbers on a text file
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.