Reputation: 39
My code currently rolls 2 dices 1,000 times and the output is how many times each number was rolled. 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...
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?
Upvotes: 1
Views: 448
Reputation: 27594
I would use a Counter
to store these kinds of dice counts, as it keeps the code nice and clean:
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
Upvotes: 0
Reputation: 51663
You can create another list of 6 values for the doubles and slightly change the rest of your code:
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:
Upvotes: 0
Reputation: 7510
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
Upvotes: 1