关于“石头剪刀布”的一点点思考
人尽皆知的“石头剪刀布”游戏,一共两个玩家,而运行结果无非三种,A赢,B赢,平局,也就是说说三种结果的概率理论上应该都是1/3,总共有3X3=9中组合方式,下面用Python模拟这个过程
#Possiblity of mora game
from random import *
mora_GamerA=["Rock","Paper","Scissor"]
mora_GamerB=["Rock","Paper","Scissor"]
count_A_win=0
count_B_win=0
count_Draw=0
Flag=1
while Flag:
round=int(input("Enetr rounds:(rounds>=10)"))
if round<10:
print("The input bureau does not meet the requirements")
Flag=0
break
else:
for round in range(1,round+1):
print("Game round {}".format(round))
guess_A=mora_GamerA[randint(0,2)]
guess_B=mora_GamerB[randint(0,2)]
"""
Attention the range of list index
"""
if (guess_A=="Rock" and guess_B=="Scissor") or (guess_A=="Paper" and guess_B=="Rock") or (guess_A=="Scissor" and guess_B=="Paper"):
count_A_win+=1
print("A is {},B is {}".format(guess_A,guess_B))
print("Gamer A is winner")
elif guess_A==guess_B:
count_Draw+=1
print("A and B is {}".format(guess_A))
print("Draw")
else:
count_B_win+=1
print("A is {},B is {}".format(guess_A,guess_B))
print("Gamer B is winner")
print("In {} rounds,gamer A have won {} rounds and gamer B have won {} rounds,{} rounds is draw.".format(round,count_A_win,count_B_win,count_Draw))
print("The possiblity of A win is {}".format(count_A_win/round))
print("The possiblity of B win is {}".format(count_B_win/round))
print("The possiblity of draw is {}".format(count_Draw/round))
Flag=int(input("Enter 1 to continue and 0 to exit :"))
if Flag!=1 and Flag!=0:
Flag=0
print("Force exit because mistake input")
运行之后发现并不是每一次的结果都完全对应1/3,但随着次数round的增加,三种概率会逐渐接近1/3这个值,因为实际上得出1/3是在理想条件下的结果,而“理想情况”需要大量的实验次数,这就是为什么在round大于100000以后取值越来越接近1/3的原因