I would make list of all the numbers in the beginning and then just add numbers in a range to the list.
It's much easier to test for something in a list than to compare each number with the previous one.



Here is a example:

import random

def randnum():
    c = []
    l = len(c)
   
   # Numbers in your list = 6
    while l < 6:
       for x in range(6):
           s = random.randrange(1, 50) # your range will be 1-49

       # test for duplicates
       if s not in c:
              c.append(s)
              l = len(c)
    c.sort()
    d = len(c)
    print ' '* 20 + 'Randoms: %s and the Bonus number: %s' % (str(c[:5]), str(c[5]))
      
    c = []
       
if __name__ == '__main__':
   q = int(raw_input('How many picks do ou need?>'))
   for x in range(q):
       randnum()

HTH
Johan




Bob Gailer wrote:
[EMAIL PROTECTED] wrote:
  
I am new to Python (and programming).  In teaching myself I wrote a
small program that will pick random lottery numbers, I want to check for
duplicates and rerun the random function.  But I am hitting a wall. 
This is what I think should work, but I still get duplicates.  TIA.
    
random.shuffle() is the predefined way to do this. But for more general 
things like this it is better to use a list to hold multiple values.

Example:

import random
picks = [] # collect unique picks
count = input("How many quick picks do you need? ")
while len(picks) < count:
  pick = random.randint(1,55)
  if not pick in picks: 
    picks.append(pick)
print picks

OR

import random
picks = [0]*55
count = input("How many quick picks do you need? ")
got = 0
while got < count:
  pick = random.randint(1,55)
  if not picks[pick]: 
    picks[pick] = 1
    got += 1
for i in range(len(picks)+1):
  if picks[i]:
    print i, 


  
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to