Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14331

Python-random number and its frequency

$
0
0

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.

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.


Viewing all articles
Browse latest Browse all 14331

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>