我想用[-3.2, 3.2)
范围内的随机值填充设备向量。以下是我为编写代码而编写的代码:
#include <thrust/random.h>
#include <thrust/device_vector.h>
struct RandGen
{
RandGen() {}
__device__
float operator () (int idx)
{
thrust::default_random_engine randEng(idx);
thrust::uniform_real_distribution<float> uniDist(-3.2, 3.2);
return uniDist(randEng);
}
};
const int num = 1000;
thrust::device_vector<float> rVec(num);
thrust::transform(
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(num),
rVec.begin(),
RandGen());
我发现向量中填充了这样的值:
-3.19986 -3.19986 -3.19971 -3.19957 -3.19942 -3.05629 -3.05643 -3.05657 -3.05672 -3.05686 -3.057
事实上,我找不到一个大于零的值!
为什么这不会从我设定的范围中生成随机值?我该如何解决这个问题?
答案 0 :(得分:3)
您必须调用randEng.discard()
函数才能使行为随机。
__device__ float operator () (int idx)
{
thrust::default_random_engine randEng;
thrust::uniform_real_distribution<float> uniDist(-3.2, 3.2);
randEng.discard(idx);
return uniDist(randEng);
}
P.S:请参阅talonmies的this answer。