【发布时间】:2012-02-26 16:21:47
【问题描述】:
我使用了this加权随机数生成器。
import random
def weighted_choice(weights):
totals = []
running_total = 0
for w in weights:
running_total += w
totals.append(running_total)
rnd = random.random() * running_total
for i, total in enumerate(totals):
if rnd < total:
return i
如下:
# The meaning of this dict is a little confusing, so here's the explanation:
# The keys are numbers and values are weights of its occurence and values - 1
# are weights of its disoccurence. You can imagine it like biased coins
# (except for 2 which is fair coin).
probabilities = { 0 : 1.0, 1 : 1.0, 2 : 0.5, 3 : 0.45, 4 : 0.4, 5 : 0.35,
6 : 0.3, 7 : 0.25, 8 : 0.2, 9 : 0.15, 10 : 0.1
}
numberOfDeactivations = []
for number in probabilities.keys():
x = weighted_choice([probabilities[number], 1 - probabilities[number]])
if x == 0:
numberOfDeactivations.append(number)
print "chance for ", repr(numberOfDeactivations)
我经常在结果中看到7、8、9、10。
是否有一些证据或保证这对概率论是正确的?
【问题讨论】:
-
什么是“经常”?你有直方图可以给我们看吗?
-
必填:xkcd.com/221
-
20 次迭代什么都没有。增加数字(数百万......)以开始查看具有统计意义的数据。对于更严肃的意图,您应该使用均匀性拟合测试:)。
-
@xralf:只有 20 次迭代,一点都不奇怪......
-
@xralf:就像我建议的那样,尝试运行一百万次,然后将结果反馈给我们。
标签: python random probability proof correctness