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)

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)

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.

Above is what i have so far, with the for loop.

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.

Did you succeed? If so, mark this solved.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.