Thread: using rand() to create a range of numbers

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    using rand() to create a range of numbers

    Say you need all the numbers between 1 and 26 in a random order (no repeats). What's the best way to go about doing that? I can think of some extremely inefficient ways...

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by keira View Post
    Say you need all the numbers between 1 and 26 in a random order (no repeats). What's the best way to go about doing that? I can think of some extremely inefficient ways...
    Fill an array with the values 1..26 and then "shuffle" it somehow. Try Googling for "random shuffle." It's easy to make a mistake and cause the shuffle to be not-quite-random, so be careful.

    If it were me, I'd do the following:

    Code:
    int array[26] = { 0 }; /* Initialize array to all zeros */
    int i;
    int index;
    
    for(i = 1; i <= 26; i++)
    {
        do
        {
            index = rand() % 26; /* Random position between 0 and 25 */
        } while(array[index] > 0);
        array[index] = i;
    }
    Basically, this loops through all the values 1..26 and selects a random spot in the array to put that value. It uses the special value 0 to mean, "No value here." If there is already some other value at that spot, i.e., if there is a non-zero value at that index, it loops, trying random indexes until it finds an unfilled spot.

    There are other ways, of course.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Thanks, I will look into shuffling.

    P.S.

    Your algorithm is flawed as it doesn't shield against repeat values returned by rand().

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by keira View Post
    Your algorithm is flawed as it doesn't shield against repeat values returned by rand().
    Yes it does.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In the C++ standard library there's actually a function random_shuffle() which is implemented using the following simple algorithm:

    http://www.nist.gov/dads/HTML/fisherYatesShuffle.html

    Edit: Also see

    http://en.wikipedia.org/wiki/Shuffli...ing_algorithms

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    C board, not C++.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What you need is pseudo-random traversal of a set algo. This algo guarantees that every member of a set will be visited at random but will be visited exactly once.

    Google it.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by prog-bman View Post
    C board, not C++.
    The algorithm is what matters - I just mentioned that the C++ standard library uses it as evidence that it's worthy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomly generate numbers
    By maybabier in forum C Programming
    Replies: 10
    Last Post: 05-06-2009, 01:13 AM
  2. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  3. Creating a UDF to create random numbers
    By rgmills in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2005, 11:22 PM
  4. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 08:16 AM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM