• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Best way to generate random numbers in C

As the subject.
1 Solution
The problem will all algorithms that use random numbers is they are predictable - given a starting point the algorithm will generate the same number sequence time and again.  So the idea is to get the random number generator to perform it's calculations frequently when not require and to "salt" the generation with additional factors such as user input and other environmental noise such as network traffic.  A good random number generating algorithm will be able to produce different results based on the environment because of the environment.  Take the environment away and the algorithm will be back to it's basic predictable state.

There are a number of sites which cover the subject - just search for Random Number Generation Theory or some variant on those words.  Most will have code samples too.
What do you mean by "best way" ??   The fastest? The most mathematically pure algorithm?    Truly random, or pseudo-random?  How many do you need?  How soon do you need them?  What tests for randomness are you going to use?     All important questions.

If you are looking for the easiest way, look into help/man pages for srand() and rand()

If you are looking for some fair shuffling with no/little repetitions, you can draw some help from these links

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

Jaime OlivaresSoftware ArchitectCommented:
remember... unless you have a quantum computer or a nice laser, all random numbers are replicatable. by adding things like the nth digit of the CPU temperature, the ambiant temperature, or the measurable quantum variance of your mother boards FSB; you can get another random number, but it is no better than a psudeo random number generated by the current time cleverly manipulated by cosine curves and other combinatorics.

I find that cat'ing random for a random amount of time, then taking a range from a random poition is the easiest way to do it. srand can be harder to use... it can actually return nothing if the random numbers it generates is not up to snuff. funny, but hey.
The truth is there's no such thing as "pure randomness" as far as generating random numbers are concerned. You can "fake" it, but it's ultimately something of a hack. I've found that for most things, though, it never really matters, so I stick with srand and rand.
srand (time (NULL));
And then rand () % SOME_MAXIMUM_NUMBER
will generate a number between 0 and SOME_MAXIMUM_NUMBER

Don't expect PURE randomness, though; that sort of thing just won't happen with rand ().
This is pretty random:

The time function returns the number of seconds after 1-1-1970, so you have to start two programs at exactly the same second to get the same number twice.

include <time.h>

to generate numbers from 1 to 10 the easiest way is


void main()
int rnum;
for (int i=0; i<10; i++)

If you don't care about repeatability and are just looking for a random number generator that generated stastically-random random numbers (that pass all tests for randomness even if they aren't) then the algorithm to go with is Blum-Blum-Shub (it's also fun to say).

There are plenty of explanations on how to implement it on the net, but the only actual C source I've seen is in "Applied Cryptography".
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now