1

我想用 range 中的随机值填充设备向量[-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

事实上,我找不到大于零的单个值!

为什么这不会从我设置的范围内生成随机值?我该如何解决?

4

1 回答 1

3

You have to call randEng.discard() function to make the behavior random.

__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: Refer to this answer by talonmies.

于 2013-09-26T09:32:53.860 回答