Computernerd
Computernerd

Reputation: 7766

Is it possible to random_shuffle an array of int elements?

I was reading up on this : http://www.cplusplus.com/reference/algorithm/random_shuffle/ and wondered if its possible to random_shuffle an array of int elements. This is my code

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a[10]={1,2,3,4,5,6,7,8,9,10};

    cout << a << endl << endl;

    random_shuffle(a[0],a[9]);

    cout<<a;
}

I got this error:

error C2893: Failed to specialize function template
    'iterator_traits<_Iter>::difference_type *std::_Dist_type(_Iter)'.

My question are:

  1. Is it possible to shuffle an int array using random_shuffle. If yes, I would like to learn how to do it.

  2. Is random_shuffle only applicable to templates?

  3. What does my error mean?

Upvotes: 31

Views: 78136

Answers (5)

Grant Wilson
Grant Wilson

Reputation: 21

Just changing the arr to a pointer does not solve the solution. This will make the array swap to one type of permutation. This means that if you rerun the program, your array will be shuffled into the exact same way as it did in the previous run.

To fix this - the function offers a third parameter which acts as a seed. So the correct implementation of the function is as follows.

1) Have a function or a lamda that generates a random number. This will act as your seed.

int myrandom (int i) { return std::rand()%i;}

Make sure to set the seed of the internal random number generator.

std::srand ( unsigned ( std::time(0) ) );

2) Insert this function as the third arguement in the random_shuffle function call.

std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);

This will result in an always random shuffled array. Make sure to include the following:

#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand

Upvotes: 2

Renato Aloi
Renato Aloi

Reputation: 320

Worked for me this way:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a[10]={0,1,2,3,4,5,6,7,8,9};

    for (unsigned i = 0; i < 10; i++)
    {
        cout << a[i];
    }
    cout << endl;

    random_shuffle(&a[0],&a[10]);

    for (unsigned i = 0; i < 10; i++)
    {
        cout << a[i];
    }
    cout << endl;
}

Upvotes: 0

Theo20185
Theo20185

Reputation: 56

Try replacing

random_shuffle(a[0],a[9]);

with

random_shuffle(&a[0], &a[10]);

From: http://www.java2s.com/Code/Cpp/STL-Basics/Userandomshufflealgorithmswitharray.htm

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

You need to pass pointers to a[0] and a[10], not the elements themselves:

random_shuffle(&a[0], &a[10]); // end must be 10, not 9

In C++11, you can use std::begin and std::end:

random_shuffle(std::begin(a), std::end(a));

Upvotes: 57

Mankarse
Mankarse

Reputation: 40603

random_shuffle takes iterators, rather than elements. Try either:

std::random_shuffle(a, a + 10);

or

std::random_shuffle(std::begin(a), std::end(a));

std::random_shuffle can be used on any pair of random access iterators, and will shuffle the elements in the range denoted by those iterators.

The error occurs because ints are not iterators, and so std::random_shuffle is unable to use the given ints as iterators.

Upvotes: 3

Related Questions