using rand() to create a range of numbers

This is a discussion on using rand() to create a range of numbers within the C Programming forums, part of the General Programming Boards category; Say you need all the numbers between 1 and 26 in a random order (no repeats). What's the best way ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,812
    C board, not C++.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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, 02: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, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21