I have just started using python and am trying to write a program that generates 200 random numbers in the range (1,50),then counts the number of occurrences of each number and prints the frequency of occurrence.

I have created the part of the code which generates the random numbers.

I need help in counting the frequency of these numbers.

However, all i would like is a push in the right direction. At the moment i am trying to do this using a for loop. How would i go about that.
Thanks

I would look from documentation of collections module what means defaultdict(int)

Edited 6 Years Ago by pyTony: n/a

I have to do it using either a for loop or a while loop, i haven't been able to get it working with a for loop yet, trying to do it with a while loop.

trying to do it with a while loop

Post your code so we have something to comment on. Also, using a dictionary or list is separate from the for() or while() loop.

Start for for solution

numbers = random_numbers(1, 50, count=200)
counts = [0 for range(50+1)]
for number in numbers:
   # add count of number
print_nicely(counts[1:], start=1) # drop zero count as it is not possible

Then make functions to generate the numbers (random_numbers) and output the results (print_nicely)

Edited 6 Years Ago by pyTony: n/a

from random import *

numbers = range( 0,10 )
count = 0
z = 0
while count <= 15:
print randint (0,9)
count += 1
for i in numbers:
z += 1
print "z =", z

I have done this at a smaller scale so it is easier to test. The range of numbers is from 0-9
and need 15 numbers to be generated.

Read up on accessing a list by the index 10.2 Lists are mutable, here, as well as the writing or updating comments in 10.3. Initialize a list (using a for loop to append zero each time) so it has one element equaling zero up to the maximum number+1. Under a new loop, generate a number using randint. The number will correspond to the list. So if the number is 5, add one to element #5 of the list, etc. I can't do it for you, but post back with code for any part that isn't working.

Edited 6 Years Ago by woooee: n/a

Thanks for your help guys. I created a list full of zero's and updated each element by adding one to it by assigning the randint function to a variable
Waz

This question has already been answered. Start a new discussion instead.