Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'random_shuffle': is not a member of 'std' error

Tags:

c++

std

I'm trying to use std::random_shuffle, and get a compilation error.

My compiler is v140(Visual Studio 2015), and I work on x64, Release mode.

My code:

#include <random>
#include <algorithm>
void foo()
{
    std::vector<int> v;
    std::random_shuffle(v.begin(), v.end());
}

The errors which I get:

error C2039: 'random_shuffle': is not a member of 'std'
error C3861: 'random_shuffle': identifier not found
  • Any idea what could the problem be?

Thanks!

like image 970
ibezito Avatar asked Jul 10 '17 13:07

ibezito


1 Answers

random_shuffle was deprecated in C++14 and completely removed in C++17.

You need to use shuffle which takes a random generator as parameter.

You can see a simple example on the site:

std::random_device rd;
std::mt19937 g(rd());

std::shuffle(v.begin(), v.end(), g);

n4190

Removing auto_ptr, random_shuffle(), And Old Stuff

...

III. What Must Die

D.12 "Random shuffle" [depr.alg.random.shuffle]

This defines random_shuffle(first, last) and random_shuffle(first, last, rng). (The latter takes a RandomNumberGenerator, whose requirements are totally different from C++11's UniformRandomNumberGenerator.)

The problem with random_shuffle(first, last) is that it's permitted to use rand(), which is permitted to be low quality. (rand() is even permitted to introduce data races, 26.8 [c.math]/5, although I'm not aware of any implementation that does so.) Constructing mt19937 urng and calling shuffle(first, last, urng) is a far superior alternative. Unlike random_shuffle(first, last), calling shuffle(first, last, urng) requires the user to be aware of the URNG's existence and state, but I argue that this is a feature, not a bug.

random_shuffle(first, last, rng) is the Knuth shuffle algorithm. It's not evil, but it's almost unusable in practice, because the "rng" function object is required to provide a very strange interface. (It's actually required to return a uniform integer distribution over a varying interval, which is difficult to do without introducing bias.) shuffle(first, last, urng) is vastly easier to use, because it directly consumes a URNG.


Visual Studio C++ standard version.

Unlike gcc or clang, Visual Studio doesn't offer the option to select the standard version, so the question that pops up is: which C++ standard does VS implement? The answer is... neither... and a little of every. Last I checked their philosophy was that they strive to slowly get to the latest standard version, but not in standard versions steps, that is they were implementing C++14 features while C+11 wasn't still fully implemented. So you will see some parts of C++11 implemented, some parts of C++14 implemented and some parts of C++17 implemented.

like image 113
bolov Avatar answered Oct 20 '22 23:10

bolov