I want to random_shuffle a vector (I know that random_shuffle is depreciated), but I get a compile error when I pass my custom random function which I copied from another thread on here. Can someone show me how to call random_shuffle using the random number generator posted below?
random_shuffle(defects.begin(), defects.end(), xorshf96);
static unsigned int x=123456789, y=362436069, z=521288629;
unsigned int xorshf96(void) { //period 2^96-1
unsigned int t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
xorshf96
does not meet the requirements of the RandomFunc
argument to std::random_shuffle
:
r
- function object returning a randomly chosen value of type convertible tostd::iterator_traits<RandomIt>::difference_type
in the interval[0,n)
if invoked asr(n)
It must take an argument and return a random number less than it. The function is in implementation of the Knuth Shuffle and that is how that algorithm works.
As a sample fix, you could provide:
unsigned int xorshf96_fixed(int n)
{
return xorshf96() % n;
}
random_shuffle(defects.begin(), defects.end(), xorshf96_fixed);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.