1

Possible Duplicate:
How exactly should I implement a shuffle or random-number algorithm for an array to display quotes in random order?

I have a small array that I need the values to be randomly shuffled around within the array. I can do this in python using random.shuffle(), but I can seem to figure out how to do it in C++.

Here is an example in python of what I want to do in C++


#!/usr/bin/python

import random

array = [1,2,3,4,5]

random.shuffle(array)

print array

Community
  • 1
  • 1
finfet
  • 226
  • 3
  • 5
  • 13
  • Same as [How exactly should I implement a shuffle or random-number algorithm for an array to display quotes in random order? ](http://stackoverflow.com/questions/3169015/how-exactly-should-i-implement-a-shuffle-or-random-number-algorithm-for-an-array), particularly the std::random_shuffle [answer](http://stackoverflow.com/questions/3169015/how-exactly-should-i-implement-a-shuffle-or-random-number-algorithm-for-an-array/3169059#3169059). – Matthew Flaschen Jan 03 '11 at 02:13
  • On the off chance you are more interested in algorithms rather than learning the C++ standard library, here's some reading to start with: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – asveikau Jan 03 '11 at 02:31

1 Answers1

15

You can use std::random_shuffle from <algorithm>.

Here's a basic example from the page:

#include <algorithm>                                                                                                    
#include <vector>                                                                                                       
#include <iostream>                                                                                                     
#include <iterator>                                                                                                     

int main()                                                                                                              
{                                                                                                                       
   const int SIZE=10;

   // create and initialize an array                                                                                                   
   int arr[] = {1,2,3,4,5,6,7,8,9,10};                                                                                  

   std::random_shuffle(arr, arr+SIZE);      

   // copy the contents of the array to output                                                                            
   std::copy(arr, arr+SIZE, std::ostream_iterator<int>(std::cout, " "));                                                
   std::cout << std::endl;                                                                                              

   // shuffling an std:: container, here it's std::vector                                                                                         
   std::vector<int> ivec(arr, arr+SIZE);                                                                                
   std::random_shuffle(ivec.begin(), ivec.end());                                                                       
   std::copy(ivec.begin(), ivec.end(), std::ostream_iterator<int>(std::cout, " "));                                     
}        

You can do it with anything that uses random access iterators, like std::vector or std::deque or just a plain array like above.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • I don't want the result to be random numbers it needs to shuffle the fixed numbers in that array. Basically shuffling the positions of the numbers in the arry – finfet Jan 03 '11 at 02:05
  • @FrozenWasteland: that's exactly what `std::random_shuffle` does...it shuffles the array's/container's contents around. – wkl Jan 03 '11 at 02:07
  • @birryree I understand the code, but where is the ostream_iterator coming from, it won't compile. – finfet Jan 03 '11 at 02:12
  • @FrozenWasteland - updated with a complete working example. SGI's example code is very barebones and doesn't show full header includes and whatnot. – wkl Jan 03 '11 at 02:19
  • Even though the original doesn't, you may want to initialize `SIZE` as `sizeof(arr)/sizeof(arr[0])`. It's less redundant. – Matthew Flaschen Jan 03 '11 at 02:24
  • 1
    As side note, if entropy is important, use the overload that allows you to pass a function object as source of randomness, or alternatively, `std::shuffle`. – jweyrich Jan 03 '11 at 03:48
  • @jweyrich - true, you can use the 2nd form that takes a functor to a randomizer, like `boost::random`. However, `std::shuffle` isn't part of standard C++ library. – wkl Jan 03 '11 at 04:02
  • @birryree: oh correct, it's on the working draft (§ 25.3.12). – jweyrich Jan 03 '11 at 04:13
  • `random_shuffle` was deprecated in C++14 and completely removed in C++17. Use `std::shuffle` instead. – KeyC0de May 12 '21 at 02:25