好的,这已经困扰了我好几年了。如果你在学校里对统计学和高等数学感到很困惑,请立刻离开。为时已晚。
好的。深呼吸。这里是规则。拿两个三十面骰子(是的,它们确实存在),并同时掷出。
- Add the two numbers
- If both dice show <= 5 or >= 26, throw again and add the result to what you have
- If one is <= 5 and the other >= 26, throw again and subtract the result from what you have
- Repeat until either is > 5 and < 26!
如果你编写一些代码(见下文),将那些骰子滚动几百万次,计算每个数字作为最终结果出现的频率,你会得到一条相当平坦的曲线,左边1的地方几乎平坦,1到60度之间有大约45°角,60以上也是平坦的。掷出30.5或更好的几率大于50%,掷出18以上的几率为80%,掷出0以上的几率为97%。
现在的问题是:是否可能编写一个计算确切值f(x)即掷出特定值的概率的程序?
背景:为了我们的角色扮演游戏《星际丛林》,我们寻找一种方法来控制随机事件。上述规则保证您尝试的事情会有更稳定的结果 :)
对于周围的极客,Python 中的代码:
import random
import sys
def OW60 ():
"""Do an open throw with a "60" sided dice"""
val = 0
sign = 1
while 1:
r1 = random.randint (1, 30)
r2 = random.randint (1, 30)
#print r1,r2
val = val + sign * (r1 + r2)
islow = 0
ishigh = 0
if r1 <= 5:
islow += 1
elif r1 >= 26:
ishigh += 1
if r2 <= 5:
islow += 1
elif r2 >= 26:
ishigh += 1
if islow == 2 or ishigh == 2:
sign = 1
elif islow == 1 and ishigh == 1:
sign = -1
else:
break
#print sign
#print val
return val
result = [0] * 2000
N = 100000
for i in range(N):
r = OW60()
x = r+1000
if x < 0:
print "Too low:",r
if i % 1000 == 0:
sys.stderr.write( %d
% i)
result[x] += 1
i = 0
while result[i] == 0:
i += 1
j = len(result) - 1
while result[j] == 0:
j -= 1
pSum = 0
# Lower Probability: The probability to throw this or less
# Higher Probability: The probability to throw this or higher
print "Result;Absolut Count;Probability;Lower Probability;Rel. Lower Probability;Higher Probability;Rel. Higher Probability;"
while i <= j:
pSum += result[i]
print %d;%d;%.10f;%d;%.10f;%d;%.10f % (i-1000, result[i], (float(result[i])/N), pSum, (float(pSum)/N), N-pSum, (float(N-pSum)/N))
i += 1