将一个骰子掷骰子转换为两个骰子

时间:2018-11-02 21:18:50

标签: python

我是一名使用python的新手,现在我的代码可以刺激掷骰子1,000次,但我只需要进行一些改进即可刺激掷骰子掷1000个骰子。时间。

这是我到目前为止可以正常运行的东西,只需要进行一些改进:

import random
test_data = [0, 0, 0, 0, 0, 0]
n = 1000
for i in range(n):
  result = random.randint(1, 6)
  test_data[result - 1] = test_data[result - 1] + 1

for i in range(0, 6):
  print ("Number of ", i+1, "'s: ", test_data[i])

关于如何使两个骰子滚动并获得与我的代码正在执行的操作类似的输出的任何建议:

Number of  1 's:  180

Number of  2 's:  161

Number of  3 's:  179

Number of  4 's:  159

Number of  5 's:  146

Number of  6 's:  175

4 个答案:

答案 0 :(得分:3)

在这种情况下,结果是212之间的数字。为了简单起见,最好保留第一个索引。

因此,test_data列表需要增加以存储12个项目,作为result,我们应该将random.randint(1, 6)称为两次(不是时间)两个),然后将它们加在一起:

import random

test_data = [0] * 12
n = 1000
for i in range(n):
  # adding up two dices
  result = random.randint(1, 6) + random.randint(1, 6)
  test_data[result - 1] += 1

for i, x in enumerate(test_data, 1):
  print ("Number of ", i, "'s: ", x)

在这里写+= 1而不是= ... + 1也更优雅,因为在这里我们避免写两次test_data[result - 1]。此外,在Python中,通常直接枚举集合,而不是索引。可以使用enumerate(iterable, start_index)生成2元组(i, n)的可迭代对象,其中索引为i,而集合中与该索引相关的元素为x

答案 1 :(得分:2)

这是解决两个无法区分的骰子的方法,这意味着投掷1和3与将3和1等同地对待。在这种方法中,我们使用dict代替list,因为对于两个(或更多!)难以区分的骰子,列表中会出现“漏洞”(像3、1这样的组合,由于我们将它们像1、3那样对待,所以永远不会出现)。

import random

counts = {}
for _ in range(1000):
    dice = tuple(sorted([random.randint(1, 6), random.randint(1, 6)]))
    counts[dice] = counts.get(dice, 0) + 1

dice现在都是两个骰子,经过排序,将3,1视为1,3,然后从列表转换为元组(基本上是不可变的列表),因此我们可以将其用作字典的键(counts)。然后我们只增加特定骰子组合的数量。

与列表不同,字典未排序,但我们确实希望按骰子显示的内容进行排序,因此我们按键=骰子进行排序:

for dice in sorted(counts.keys()):
    print("{} occurred {} times".format(dice, counts[dice]))

这给您:

(1, 1) occurred 22 times
(1, 2) occurred 53 times
(1, 3) occurred 47 times
(1, 4) occurred 55 times
(1, 5) occurred 55 times
(1, 6) occurred 50 times
(2, 2) occurred 27 times
(2, 3) occurred 64 times
(2, 4) occurred 58 times
...

答案 2 :(得分:1)

您可以使用numpy,此解决方案允许您指定任意数量的骰子:

import numpy as np

no_of_dice = 2
sides_on_die = 6
rolls = 1000
dice = np.array([0]*rolls)

for i in range(no_of_dice):
    dice += np.random.randint(1,sides_on_die+1,rolls)
data = np.bincount(dice)

for i in range(no_of_dice,no_of_dice*sides_on_die+1):
    print ("Number of ", i, "'s: ", data[i])

收益:

Number of  2 's:  26
Number of  3 's:  55
Number of  4 's:  100
Number of  5 's:  106
Number of  6 's:  139
Number of  7 's:  152
Number of  8 's:  135
Number of  9 's:  104
Number of  10 's:  87
Number of  11 's:  64
Number of  12 's:  32

答案 3 :(得分:1)

如果允许您使用其他python模块,那么random可以利用collections.Counter进行计数。通过从random.randint()切换到random.choices,您可以一次投掷两个骰子:

import random
from collections import Counter


def roll_n_dice_of_sides_x_times(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields 'x' values that 
    hold the sum of the 'x' dice rolls.""" 
    r = range(1,sides+1)
    yield from (sum(random.choices(r,k=n)) for _ in range(x))

# this does allthe counting and dice throwingof 1000 2-6sided-dice-sums
c = Counter(roll_n_dice_of_sides_x_times(2,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for eyes,count in sorted(c.items()):
    print(f"Number of {eyes:>3}'s : {count}")

输出:

Number of   2's : 24
Number of   3's : 51
Number of   4's : 66
Number of   5's : 115
Number of   6's : 149
Number of   7's : 182
Number of   8's : 153
Number of   9's : 116
Number of  10's : 68
Number of  11's : 58
Number of  12's : 18

Doku:

  • collections.Counter
    • 它是一个字典-您向它提供可迭代的内容,并且int会计算可迭代的每个元素出现的频率:
        print(Counter( [1,2,2,3,3,3,4,4,4,4] ) )

        # Counter({4: 4, 3: 3, 2: 2, 1: 1})

如果您想要单个骰子结果,则可以修改代码以不对骰子求和,而是在生成随机数时传递元组。我对它们进行了排序,以便(5,4,5)与(4,5,5)相同:

import random
from collections import Counter

def roll_n_dice_of_sides_x_times_no_sum(n,x,sides=6):
    """Rolls 'n' dices with 'sides' sides 'x' times. Yields a sorted tuple 
    of the dice throwsof all  'x' dice rolls.""" 
    r = range(1,sides+1)

    # instead of summing, create a tuple (hashable, important for Counter)
    # and return that sorted, so that 4,4,5 == 5,4,4 == 4,5,4 throw:
    yield from ( tuple(sorted(random.choices(r,k=n))) for _ in range(x))

# throw 3 6-sided dice 1000 times and count:
c = Counter(roll_n_dice_of_sides_x_times_no_sum(3,1000))

# print the sorten (key,value) tuples of the Counter-dictionary. Sort by 
# how much eyes thrown, then amount of occurences
for dice,count in sorted(c.items()):
    print(f"{dice} occured {count} times")

输出(缩短):

(1, 1, 1) occured 3 times
(1, 1, 2) occured 14 times
[...] 
(2, 3, 5) occured 32 times
(2, 3, 4) occured 21 times
[...]
(4, 6, 6) occured 10 times
(5, 5, 5) occured 3 times
(5, 5, 6) occured 20 times
(5, 6, 6) occured 9 times
(6, 6, 6) occured 4 times
相关问题