[英]C++ STL std::random_shuffle cannot pass custom random number generator
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. 我想对向量进行random_shuffle(我知道random_shuffle已弃用),但是当我传递自定义随机函数(从另一个线程复制到此处)时,出现编译错误。 Can someone show me how to call random_shuffle using the random number generator posted below?
有人可以教我如何使用下面发布的随机数生成器调用random_shuffle吗?
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
: xorshf96
不满足std::random_shuffle
的RandomFunc
参数的要求:
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)
r
如果以r(n)
调用,则返回间隔[0,n)
随机转换为std::iterator_traits<RandomIt>::difference_type
类型的值的函数对象
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.
该功能是在Knuth Shuffle的实现中实现的,这就是该算法的工作方式。
As a sample fix, you could provide: 作为示例修复,您可以提供:
unsigned int xorshf96_fixed(int n)
{
return xorshf96() % n;
}
random_shuffle(defects.begin(), defects.end(), xorshf96_fixed);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.