gpt4 book ai didi

r - 为什么 runif() 的唯一值比 rnorm() 少?

转载 作者:行者123 更新时间:2023-12-04 01:10:00 26 4
gpt4 key购买 nike

如果您运行如下代码:

length(unique(runif(10000000)))
length(unique(rnorm(10000000)))

你会看到只有大约 99.8% 的 runif 值是唯一的,但 100% 的 rnorm 值是唯一的。我认为这可能是因为范围受限,但是将 runif 的范围提高到 (0, 100000) 不会改变结果。连续分布应该有重复的概率 = 0,我知道在浮点精度中情况并非如此,但我很好奇为什么我们没有看到两者之间的重复次数相当接近。

最佳答案

这主要是由于默认 PRNG 的属性(事实上,runif 的范围小于 rnorm,因此即使 RNG 没有)。在 ?Random 中有些倾斜地讨论了它。 :

Do not rely on randomness of low-order bits from RNGs. Most of the supplied uniform generators return 32-bit integer values that are converted to doubles, so they take at most 2^32 distinct values and long runs will return duplicated values (Wichmann-Hill is the exception, and all give at least 30 varying bits.)



举个例子:
sum(duplicated(runif(1e6))) # around 110 for default generator
## and we would expect about almost sure duplicates beyond about
qbirthday(1 - 1e-6, classes = 2e9) # 235,000

更改为 Wichmann-Hill 生成器确实减少了重复的机会:
RNGkind("Wich")  
sum(duplicated(runif(1e6)))
[1] 0
sum(duplicated(runif(1e8)))
[1] 0

关于r - 为什么 runif() 的唯一值比 rnorm() 少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51425656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com