Python-случайное число и его частотаPython

Программы на Python
Гость
Python-случайное число и его частота

Сообщение Гость »


Функция 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 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.


Источник: https://stackoverflow.com/questions/209 ... -frequency

Вернуться в «Python»