在 Python 中模拟滚动 2 个骰子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33069476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:42:41  来源:igfitidea点击:

simulating rolling 2 dice in Python

pythonpython-3.xrandom

提问by A.Boland

I have been asked to simulate rolling two fair dice with sides 1-6. So the possible outcomes are 2-12.

我被要求模拟掷两个面为 1-6 的公平骰子。所以可能的结果是2-12。

my code is as follows:

我的代码如下:

def dice(n):
    x=random.randint(1,6)
    y=random.randint(1,6)
    for i in range(n):
        z=x+y
    return z

My problem is that this is only returning the outcome of rolling the dice 1 time, so the outcome is always 2-12. I want it to return the sum of rolling the dice (n) times.

我的问题是这只是返回掷骰子 1 次的结果,所以结果总是 2-12。我希望它返回掷骰子 (n) 次的总和。

Does anyone have any suggestions for me?

有人对我有什么建议吗?

回答by Martijn Pieters

Roll the dice in the loop:

在循环中掷骰子:

def dice(n):
    total = 0
    for i in range(n):
        total += random.randint(1, 6)
    return total

The +=augmented assignment operator basically comes down to the same thing as total = total + random.randint(1, 6)when summing integers (it is slightly more complicated than that, but that only matters for mutable objects like lists).

+=增强赋值运算符基本上可以归结为同样的事情,total = total + random.randint(1, 6)总结整数(这是略多于复杂,但只有事项可变对象如列表)时。

You could even use a generator expressionand the sum()function:

您甚至可以使用生成器表达式sum()函数

def dice(n):
    return sum(random.randint(1, 6) for _ in range(n))

This basically does the same thing as the forloop in the first example; loop ntimes, summing up that many random numbers between 1 and 6 inclusive.

这基本上for与第一个示例中的循环执行相同的操作;循环n时间,总结了 1 到 6 之间的许多随机数。

If instead of rolling ntimes, you need to produce nresults of 2 dice rolls, you still need to roll in the loop, and you need to add the results to a list:

如果不是滚动n次数,你需要产生n2个骰子滚动的结果,你仍然需要在循环中滚动,你需要将结果添加到列表中:

def dice(n):
    rolls = []
    for i in range(n):
        two_dice = random.randint(1, 6) + random.randint(1, 6)
        rolls.append(two_dice)
    return rolls

This too can be written out more compactly, with a list comprehension:

这也可以用列表理解更紧凑地写出来:

def dice(n):
    return [random.randint(1, 6) + random.randint(1, 6) for _ in range(n)]

You could also use random.choice()from a list of generated sums; these are automatically weighted correctly; this basically pre-computes the 36 possible dice values (11 unique), each with equal probability:

您还可以使用random.choice()生成的总和列表;这些会自动正确加权;这基本上预先计算了 36 个可能的骰子值(11 个唯一值),每个骰子的概率相等:

from itertools import product

two_dice_sums = [a + b for a, b in product(range(1, 7), repeat=2)]

def dice(n):
    return [random.choice(two_dice_sums) for _ in range(n)]

Either way, you'll get a list with nresults:

无论哪种方式,您都会得到一个包含n结果的列表:

>>> dice(5)
[10, 11, 6, 11, 4]
>>> dice(10)
[3, 7, 10, 3, 6, 6, 6, 11, 9, 3]

You could pass the list to the print()function to have these printed on one line, or on separate lines:

您可以将列表传递给print()函数以将这些打印在一行或单独的行上:

>>> print(*dice(5))
3 7 8 6 4
>>> print(*dice(5), sep='\n')
7
8
7
8
6