Skip to main content
 首页 » 编程设计

python之如何模拟掷骰子

2025年05月04日13虾米姐

如何模拟 UNFAIR 6 面骰子的滚动。不是每一方都有平均的机会上来(1/6 = 16.7%),中间的数字应该受到青睐。应该有 20% 的机会掷出 2、3、4 或 5,而掷出 1 或 6 的机会只有 10%。 谢谢

请您参考如下方法:

另一种可能性:

import random 
result = random.choices([1, 2, 3, 4, 5, 6], weights=[10, 20, 20, 20, 20, 10])[0] 

参见 documentation .