Thread: How to pick a random number between x and y

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    How to pick a random number between x and y

    I'm trying to learn C-programming by myself and this is the first thing i'm really having trouble with. So how can i pick a random number for example between 1000 and 10 000?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    search for rand() function

    If you are just starting to learn C programming I think you should better start learning the basics first

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Quote Originally Posted by mahaju View Post
    search for rand() function

    If you are just starting to learn C programming I think you should better start learning the basics first
    I'm past the basics but i need something more than a mere name for the function.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    36
    Quote Originally Posted by CMEE View Post
    I'm past the basics but i need something more than a mere name for the function.
    Try this website
    srand - C++ Reference
    It has a lot of C++ references, but since it is backward compatible with C you will find information about many C functions as well

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The basic formula is MAX - MIN + MIN. So something like this would work:

    Code:
    int num_between_x_and_y = (rand() % (X - Y)) + Y;
    If you understand what you're doing, you're not learning anything.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The most un-biased method of doing it is to use the rejection method. This is as follows:
    Code:
    // Generates random number between min and max, inclusive.
    int random(int min, int max)
    {
        int range, result, cutoff;
    
        if (min >= max)
            return min; // only one outcome possible, or invalid parameters
        range = max-min+1;
        cutoff = (RAND_MAX / range) * range;
    
        // Rejection method, to be statistically unbiased.
        do {
            result = rand();
        } while (result >= cutoff);
    
        return result % range + min;
    }
    The theory with the rejection method is easier to understand if you imagine a random number generator that generates a 4-bit random number and you want a random number with six equally probably outcomes (say for a die roll).
    Throwing away the loop and using % alone, you get:

    • 0 if rand() was 0, 6, or 12
    • 1 if rand() was 1, 7, or 13
    • 2 if rand() was 2, 8, or 14
    • 3 if rand() was 3, 9, or 15
    • 4 if rand() was 4 or 10
    • 5 if rand() was 5 or 11


    Notice how there are three ways to get 0-3 but only two ways of getting 4-5. This is statistically biased towards the lower numbers! So the rejection method would throw away any random number of 12 or above and try again. That way there are exactly two ways of arriving at each outcome. (The calculation for cutoff calculates 12 in this case due to the integer division result of 16 / 6 being 2, and 2 * 6 = 12)

    About the loop:
    Here it is ignoring 4 values out of 16 so it will loop around again 1/4th of the time, but in a real situation it loops so few times you do not have to worry about it. E.g. when getting a random number for a die roll using random number generator that gives a 15-bit output, it loops only 4 out of 32767 times. That's small enough that you don't need to worry about it. The chance of that looping even 4 times is 0.000000000018%

    Last but not least, make sure that you call srand only once in your program, preferably inside main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Random Pick from a file
    By mcorn in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2004, 03:53 PM
  3. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  4. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

Tags for this Thread