The function
randint
from the random module can be used to produce random numbers. A call onrandom.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 onrandint
to simulate rolling a pair of dice. Compute the sum of the two dice, and record the number of times each value appears.The output should be two columns. One displays all the sums (i.e. from 2 to 12) and the other displays the sums' respective frequencies in 1000 times.
My code is shown below:
import random
freq=[0]*13
for i in range(1000):
Sum=random.randint(1,6)+random.randint(1,6)
#compute the sum of two random numbers
freq[sum]+=1
#add on the frequency of a particular sum
for Sum in xrange(2,13):
print Sum, freq[Sum]
#Print a column of sums and a column of their frequencies
However, I didn't manage to get any results.
You shouldn't use Sum
because simple variables should not be capitalized.
You shouldn't use sum
because that would shadow the built-in sum()
.
Use a different non-capitalized variable name. I suggest diceSum
; that's also stating a bit about the context, the idea behind your program etc. so a reader understands it faster.
You don't want to make any readers of your code happy? Think again. You asked for help here ;-)