2013-09-26 25 views
0

我想用范围为[-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

[使用Thrust在0和1.0之间生成一个随机数向量]可能的副本(http://stackoverflow.com/questions/12614164/generating-a-random-number-vector-between-0-and-1- 0-使用推力) – talonmies

回答

2

您必须拨打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