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.
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.