简体   繁体   English

双掷骰子

[英]Double Rolls on a Dice

My code currently rolls 2 dices 1,000 times and the output is how many times each number was rolled.我的代码目前掷 2 个骰子 1,000 次,输出是每个数字掷出的次数。 However, I need to improve the code so that the output also includes every time a double is rolled.但是,我需要改进代码,以便每次滚动时输出也包括在内。 An example of what I need the output to be similar to is:我需要输出类似于的一个例子是:

Snake Eyes were rolled 5 times

Double 2's were rolled 8 times

Double 3's were rolled 17 times 

And so on up til 6...依此类推,直到 6...

This is my current code which is working perfectly:这是我当前运行完美的代码:

import random

test_data = [0] * 12
n = 1000
for i in range(n):
    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)

The output is:输出是:

Number of  1 's:  0
Number of  2 's:  28
Number of  3 's:  56
Number of  4 's:  82
Number of  5 's:  107
Number of  6 's:  130
Number of  7 's:  172
Number of  8 's:  133
Number of  9 's:  115
Number of  10 's:  93
Number of  11 's:  56
Number of  12 's:  28

How can I improve my code so that "doubles" also appear?如何改进我的代码以便“双打”也出现?

split the line分割线

 result = random.randint(1, 6) + random.randint(1, 6)

into进入

roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)

now you can test for doubles with:现在你可以测试双打:

if roll1 == roll2:
   #log double

You can create another list of 6 values for the doubles and slightly change the rest of your code:您可以为双打创建另一个包含 6 个值的列表,并稍微更改其余代码:

import random

doubles = [0] * 6
test_data = [0] * 12
n = 1000

for i in range(n):
    a,b = random.randint(1, 6) , random.randint(1, 6) # create 2 distinct ones
    result = a+b                                      # and the sum
    test_data[result - 1] += 1
    if a == b:                                        # if a==b count up
        doubles[a-1] += 1


for i, x in enumerate(test_data, 1):
     if i > 1: # no sum of 1 possible
         print ("Number of {:>2}'s: {:>6}".format(i,x))

# output for doubles:
for i,x in enumerate(doubles,1):
    # python ternary operator, see link below
    print( ("Snake eyes were rolled {cnt} times" if i == 1 
            else "Double {}'s were rolled {cnt} times").format(i,cnt=x))

Output:输出:

Number of  2's:     28
Number of  3's:     64
Number of  4's:     72
Number of  5's:    120
Number of  6's:    139
Number of  7's:    139
Number of  8's:    137
Number of  9's:    114
Number of 10's:     93
Number of 11's:     58
Number of 12's:     36
Snake eyes were rolled 28 times
Double 2's were rolled 28 times
Double 3's were rolled 25 times
Double 4's were rolled 23 times
Double 5's were rolled 28 times
Double 6's were rolled 36 times

Readup:阅读:

I would use a Counter to store these kinds of dice counts, as it keeps the code nice and clean:我会使用Counter来存储这些类型的骰子计数,因为它使代码保持整洁:

from collections import defaultdict, Counter
import random

dice_combined = Counter()
dice_individual = defaultdict(Counter)

for i in range(1000):
  a = random.randint(1, 6)
  b = random.randint(1, 6)
  dice_combined[a+b] += 1
  dice_individual[a][b] += 1

# print report:
for i in dice_combined:
  print(i, 'was rolled', dice_combined[i], 'times')

for i in range(1, 6, 1):
  print('double', i, 'was rolled', dice_individual[i][i], 'times')

Sample Report :样本报告

2 was rolled 25 times
3 was rolled 49 times
4 was rolled 77 times
5 was rolled 99 times
6 was rolled 150 times
7 was rolled 169 times
8 was rolled 140 times
9 was rolled 100 times
10 was rolled 101 times
11 was rolled 67 times
12 was rolled 23 times
double 1 was rolled 25 times
double 2 was rolled 29 times
double 3 was rolled 21 times
double 4 was rolled 23 times
double 5 was rolled 28 times

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM