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.
2. If you're on Linux (regardless of language), consider reading from /dev/urandom. This will give you truly random numbers.
3. Regardless of how you generate the random (or pseudo-random) number, you'll usually constrain it to a specific range with the "modulus" operator
C/C++ EXAMPLES:
a) inum = rand () % 20
<= returns a number between 0 .. 19
b) inum = rand () % 21
<= Returns a number between 0 .. 20
c) inum = (rand () % 20) + 1
<= Returns a number between 1 .. 20
Can you define what you really require?
For most tasks pseudo-random numbers are sufficient. Why do you need true random numbers? It can be done but its isn't easy.
What language are you going to use for this?
Reading /dev/random /dev/urandom gives you random bytes, using "noise" from device interrupts etc, hence close-to-true-random bytes. Using /dev/random your program may have to wait until sufficient "noise" is gatherd by the kernel, while /dev/urandom will use the noise if available, but will mever block if there's not sufficient "noise".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.