【发布时间】:2021-07-28 01:45:16
【问题描述】:
在棋盘游戏 Catan 中,您每回合掷 2 个六面骰子,可能的结果从 2 到 12,并且在 100,000 次掷中时总和的分布应该是这样的:
***CATAN DICE***
2 *****5998
3 ********8170
4 ********8170
5 **********10299
6 ************12283
7 **************14341
8 ************12331
9 **********10149
10 ********8090
11 ******6033
12 ****4068
但是,我只有在使用randint(0, 6) 的骰子是从 0 到 6 的七面时才会得到这个结果。
当我使用randint(1,6) 时,分布如下:
***CATAN DICE***
2 **2771
3 *****5560
4 *****5560
5 ********8410
6 ***********11088
7 **************14132
8 ****************16491
9 *************13768
10 ***********11163
11 ********8220
12 *****5616
这是错误的,8 不太可能出现在一对六面骰子中。
¿我的代码有问题吗?,¿它可能与 randint() 的工作方式有关吗?
这是我的代码:
from random import randint
print("***CATAN DICE***")
normal = [0,0,0,0,0,0,0,0,0,0,0]
for i in range(0, 100000):
a=randint(0, 6)
b=randint(0, 6)
throw = a+b
if throw == 2:
normal[0]+=1
if throw == 3:
normal[1]+=1
if throw == 3:
normal[2]+=1
if throw == 4:
normal[3]+=1
if throw == 5:
normal[4]+=1
if throw == 6:
normal[5]+=1
if throw == 7:
normal[6]+=1
if throw == 8:
normal[7]+=1
if throw == 9:
normal[8]+=1
if throw == 10:
normal[9]+=1
if throw == 11:
normal[10]+=1
i=0
j=0
for i in range(len(normal)):
print(i+2, end = " ")
for j in range(int(normal[i]/1000)):
print("*",end="")
print(f"{normal[i]}\n")
【问题讨论】:
-
首先你应该有 randint(1,6) 而不是 randint(0,6)
-
如果你使用 numpy,你可以这样做:
np.unique(np.random.randint(1, 7, [100000, 2]).sum(1), return_counts = True) -
我尝试了这两个:randint(1,6) 和 randint (0,6),但是当我使用 1,6 时,我得到 8 的频率更高,这是错误的。
-
您的问题在于您的
if语句。您有两次出现if throw == 3:。你的双重5560是一个提示。 -
这里没有正态分布。两个离散制服的总和具有离散三角形分布。
标签: python python-3.x distribution dice