Random 推力均匀随机分布为什么会产生错误的值?
我想用Random 推力均匀随机分布为什么会产生错误的值?,random,cuda,thrust,Random,Cuda,Thrust,我想用[-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
[-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());
事实上,我找不到一个大于零的值
为什么这不会从我设置的范围中生成随机值?如何修复此问题?您必须调用
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);
}
\uuuu设备\uuuuu浮点运算符()(int idx)
{
推力:默认随机发动机随机;
推力:均匀实分布单位距离(-3.2,3.2);
随机丢弃(idx);
返回单一区(兰登);
}
备注:由Talonmes参考。可能的副本
__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);
}