给定 [0, 2^64) 范围内的均匀分布的随机数生成器,是否有任何有效的方法(在 GPU 上)为某些 k < 2^64 的范围 [0,k) 构建随机数生成器?
一些不起作用的解决方案:
// not uniformly distributed in [0, k)
myRand(rng, k) = rng() % k;
// way too much branching to run efficiently on a gpu
myRand(rng, k) =
uint64_t ret;
while((ret = rng() & (nextPow2(k)-1)) >= k);
return ret;
// only 53 bits of random data, not 64. Also I
// have no idea how to reason about how "uniform"
// this distribution is.
myRand(doubleRng, k) =
double r = doubleRng(); // generates a random number in [0, 1)
return (uint64_t)floor(r*k);
如果差异足够小(例如,在 1/2^64 内),我愿意妥协不均匀性。