hello, long time lurker first time poster!

currently writing a program that generates 100 random integer values in the range of 1 - 50, then displays how many of those numbers are in the range 1-10, 11-20 etc etc.

i generated the numbers with no problem, but im not too sure how to tackle the next part of the program.

any suggestions on how my program should be structured would be greatly appreciated!

This is what I would do. Create a loop for generating the numbers, and inside that loop, if the number is 1-10, then a = a + 1, 2-20, then b = b+ 1, etc., until you reach the end. Such as this [Note it's untested, this is just at template to get your brain working]:

import random
x = 0
while x <  (the number you want):
     rannum = random.randint(a, b) Where a and b are the numbers you define
     if rannum <= 10 and rannum >= 1:
          a = a + 1
     if rannum < 20 and rannum > 11:
          b = b + 1
print "The number of times a number was generated between 1 and 10 was: %d" %(a)
print "The number of times a number was generated between 11 and 20 was: %d" %(b)

Note that I'm not sure that %d is the one you use for integers. Hope this helps! :D

hondros -

thank you, this helps alot! ill have a tinker and see if i can get it running :D

hondros -

thank you, this helps alot! ill have a tinker and see if i can get it running :D

Don't forget to set a and b to zero outside the loop.

Ah yes, I forgot to put that in there. Oh well. :D Thanks for pointing that out for him.

This article has been dead for over six months. Start a new discussion instead.