Best way to generate random numbers in C

Posted on 2005-04-26
Last Modified: 2010-04-15
As the subject.
Question by:RomanRega
    LVL 19

    Expert Comment

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

    Expert Comment

    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.

    LVL 45

    Expert Comment

    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

    LVL 55

    Expert Comment

    by:Jaime Olivares

    Expert Comment

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

    Expert Comment

    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 ().
    LVL 1

    Expert Comment

    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>

    LVL 4

    Expert Comment

    to generate numbers from 1 to 10 the easiest way is


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

    LVL 11

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now