下面是公开课《数学与魔术》的第4集《运气偏向一方的骰子游戏》视频:
视频里没有详细说明各种骰子的点数,但是经过我专业的观察,终于得出了各种骰子的点数。
这些骰子的点数设计得非常精妙,任意两颗骰子都没有相同的点数,所以不会出现平局的情况。
于是我写了一段python代码来求出骰子的胜率,代码如下:
# -*- coding : utf-8 -*- #骰子的点数 dices = { "red" : [4, 4, 4, 4, 4, 9], "blue" : [2, 2, 2, 7, 7, 7], "green" : [0, 5, 5, 5, 5, 5], "yellow" : [3, 3, 3, 3, 8, 8], "purple" : [1, 1, 6, 6, 6, 6], } #返回第一种颜色赢第二种颜色的次数 def get_win_times(color1, color2): times = 0 for i in dices[color1]: for j in dices[color2]: if i > j: times += 1 return times if __name__ == "__main__": #输出赢的概率 colors = ["red", "blue", "green", "yellow", "purple", "red"] for i in range(len(colors)-1): color1 = colors[i] color2 = colors[i + 1] win_times = get_win_times(color1, color2) print "%6s beats %-6s : %d/36"%(color1, color2, win_times) ''' 执行结果: red beats blue : 21/36 blue beats green : 21/36 green beats yellow : 20/36 yellow beats purple : 20/36 purple beats red : 20/36 '''
一个骰子有6面,投两个骰子就可以得出6*6=36种局面,通过判断局面就可以得出骰子赢的次数,再除以36就能得出赢的概率了。
从执行的结果可以看出,如果用红色骰子跟蓝色骰子玩,红色骰子有21/36的概率能赢,也就是视频里面所说的7/12,约等于58%。
理论上讲,投够一定的次数后,红色骰子必定赢蓝色骰子。
.
2014/01/06 01:07:32
这个开眼界了!!
2013/12/15 23:37:11
原来概率是这么用的呀。哈哈,好神奇。