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 randomfreq=[0]*13for 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 sumfor 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.