Shuffling an array

VernonDozier 1 Tallied Votes 769 Views Share

This snippet is similar to my snippets on how to create random numbers without repeats, particularly this one.

This snippet is different in that it takes an already existing array and "shuffles" it so that the order appears random. No checking is done to make sure that there are no duplicates in the array.

Try changing ARRAY_SIZE and NUM_SWAPS in the code and see how that affects the "randomness" of the array.

/* This is a shuffling program.  It takes an array of integers and "shuffles"
   it so the order is more or less "random" by swapping array indexes.  The more
   swaps you make, the more random the order will appear.

   This particular example simply takes an array of 10 integers containing the
   numbers 0 through 9 and shuffles it.  The concept is easily to expand on
   though and you can easily make this program work for doubles, floats, chars, etc.,
   or make a template from it.

   srand, rand, and time appear to be part of the iostream library, but I have
   included ctime and cstdlib because you need them for the srand, time, and rand
   functions if you are not including iostream.
*/

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main ()
{
    srand (time (NULL));

    const int ARRAY_SIZE = 10;
    const int NUM_SWAPS = 1000;

    // create unshuffled array
    int array[ARRAY_SIZE];
    for (int i = 0; i < ARRAY_SIZE; i++)
        array[i] = i;

    int swapIndex1, swapIndex2, temp;

    for (int i = 0; i < NUM_SWAPS; i++)
    {
        swapIndex1 = rand () % ARRAY_SIZE;
        swapIndex2 = rand () % ARRAY_SIZE;

        if (swapIndex1 == swapIndex2)
            i--;  // indexes are the same.  Nothing to swap, so don't increment swap
                  // counter i.  for-loop increments i, so decrement here
        else
        {
            // swap the values
            temp = array[swapIndex1];
            array[swapIndex1] = array[swapIndex2];
            array[swapIndex2] = temp;
        }
     }

     // display
     for (int i = 0; i < ARRAY_SIZE; i++)
         cout << array[i] << endl;

     return 0;
}