0

I am learning Python from the book Exploring Python by Timothy Budd. One of the exercises from this chapter is this:

15. The function randint from the random module can be used to produce random numbers. A call on random.randint(1, 6), for example, will produce the values 1 to 6 with equal probability. Write a program that loops 1000 times. On each iteration it makes two calls on randint to simulate rolling a pair of dice. Compute the sum of the two dice, and record the number of times each value appears. Afterthe loop, print the array of sums. You can initialize the array using the idiom shown earlier in this chapter:

times = [0] * 12 # make an array of 12 elements, initially zero

I am able to print the sum in the array, but I have not understood the concept of recording the number of times each value appears. Also, what purpose would times = [0] serve? Here is my code for printing the sum:

 #############################################
#   Program to print the sum of dice rolls  #
#############################################

from random import randint
import sys


times = [0] * 12

summation = []

def diceroll():

    print "This program will print the"
    print "sum of numbers, which appears"
    print "after each time the dice is rolled."
    print "The program will be called 1000 times"



    for i in range(1,1000):

        num1 = randint(1,6)
        num2 = randint(1,6)

        sum = num1 + num2
        summation.append(sum)
        #times[i] = [i] * 12
    print summation
    #print times


diceroll()

2 Answers 2

1

times[0] * 12 initiates a list of 12 elements with value 0. What you want to do next is

times[s] += 1 #adds 1 to the number of times s occurs

This is similar to using a dict to encode a value, but its simpler.

times = [0] initializes a list called times of length 1 and value 0. The idiom times = [0] * 12 means times is a list of 12 zeroes.

If you want to be able to use this without keyerror when num1==num2==6, you need to do times = [0]*13 because python is a 0 indexed system.

Sidenote: Don't use sum as a variable name because it is a builtin function (predefined) and you don't want to override it. Use times[sum([num1,num2])] += 1 or times[num1+num2] instead.

Sign up to request clarification or add additional context in comments.

10 Comments

so summation[sum(num1 + num2)] +=1 would give me the sum of rolls of dice.
num1 + num2 is already a sum, so there's no need to call sum on it. It's a TypeError anyway, as sum expects a list. But yes, putting times[sum] += 1 in your loop instead of summation.append(sum) would give you the sum of dice rolls.
No, it will update the list summation to record the right numbers. summation[i] will give you the number of sums adding up to i (if i is < 12).
Since python lists are 0-indexed, you will actually need to initiate times as a list of 13 if you want to be able to call times[12]
oh yes, my mistake, I wanted to write Sum, instead wrote sum. Thanks for pointing this out.
|
0

To count number of occurrences:

times = [0]*13
for _ in range(1000):
    sum_ = randint(1, 6) + randint(1, 6)
    times[sum_] += 1

print(times[2:])

Possible values of sum_ are 2...12 including. Possible times indexes are 0...12 including.

times[i] corresponds to a number of occurrences of sum_ == i among 1000 tries.

Note: times[0] and times[1] are always zero because sum_ > 1

[x]*3 produces [x, x, x] It is a nicer version of:

L = []
L.append(x)
L.append(x)
L.append(x)

Additional comments about your code:

  • print something is an error on Python 3
  • for i range(1, 1000) is wrong. It produces i from 1 to 999 including (999 iterations instead of 1000 as required). range doesn't include upper limit as it customery for intervals in programming.

4 Comments

times[sum_-1] += 1, is that -1 with the purpose of reversing the order?
Sebastian: Yes, range has to be from 0 to 1001, to iterate 1000 times. Also, for the first sum, times = 0, but when first sum appears, then time or occurence is not zero, i guess this is an error.
@vamosrafa: I've simplified the code a bit. There is no error as far as I can see. I've initially considered the times = [0]*12 to be a requirement for the exercise.
Sebastian: i have understood the problem now. Suppose the sum is 4, and 4 occurs 24 times, so in times array, the number of occurerces of sum is printed. And the lenght will be 12, because dice is from 1 to 6, so two dice = 12. Also, the sum of the element of array would be 1000. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.