4
#include <random>
#include <algorithm>
#include <vector>

int main() {
    std::vector < int > a = {1, 2, 3};
    std::mt19937 generator;
    std::random_shuffle(a.begin(), a.end(), generator);
}

I am trying to compile this code with g++ -std=c++0x, receiving a huge compiler dump ending with

/usr/include/c++/4.9.2/bits/random.h:546:7: note:   candidate expects 0 arguments, 1 provided

Any way of doing it correctly?

whoever
  • 575
  • 4
  • 18
  • Thx, worked like a charm. Could you post it as an answer in order for me to accept it? – whoever Nov 19 '14 at 12:49
  • 1
    You could use a wrapper function or lambda; the `RandomFunc` parameter expects something which will itself take a parameter `n`, and the C++11 random number facilities all have their range parameters baked into their instances. But chris' answer is best, of course. – Rook Nov 19 '14 at 12:54

1 Answers1

7

The overload of std::random_shuffle you're trying to use takes a "RandomFunc":

function object returning a randomly chosen value of type convertible to std::iterator_traits<RandomIt>::difference_type in the interval [0,n) if invoked as r(n)

In C++14, std::random_shuffle is deprecated in favour of std::shuffle, which takes a generator rather than that "RandomFunc". Simply change the function to std::shuffle.

chris
  • 60,560
  • 13
  • 143
  • 205