我想在已知 CDF 的情况下快速生成离散随机数。本质上,该算法是:
- 构造 CDF 向量(从 0 开始到 1 结束的递增向量)
cdf
- 生成一个 uniform(0, 1) 随机数
u
- 如果
u < cdf[1]
选1 - 否则如果
u < cdf[2]
选择2 - 否则,如果
u < cdf[3]
选择 3 *...
- 如果
例子
首先生成一个cdf:
cdf = cumsum(runif(10000, 0, 0.1))
cdf = cdf/max(cdf)
接下来生成N
统一的随机数:
N = 1000
u = runif(N)
现在对值进行采样:
##With some experimenting this seemed to be very quick
##However, with N = 100000 we run out of memory
##N = 10^6 would be a reasonable maximum to cope with
colSums(sapply(u, ">", cdf))