Thread: python loop2

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10

    Arrow python loop2


    Hi
    Can someone help me how to pick two set of 6 different random numbers . I tried but the while loop is not exititng.

    Thanks in advance

    Here is my code.
    Code:
    import sys
    import random
    array=[]
    x=0
    while x!=2: how to get multiple lines for 6 random numbers
     for i in range(6):
        temp=random.randrange(1,50)
        while temp in array:
            temp=random.randrange(1,50)
        array+=[temp]
     x+=1
     array.sort(cmp)
     for a in array:
      print array,
    Last edited by luckyboy; April 29th, 2004 at 09:15 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    firstly, please wrap your code in [code] tags. It will preserve the indentation, and not using it makes Python code virtually unreadable.

    Secondly, what exactly do you mean by pick 6 different random numbers twice? Do you mean two sets of numbers without duplicates? Or two sets of random numbers, but there may be duplicates between sets? Or the same set, twice?

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10
    Yes i mean 2 sets of 6 random numbers
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    I would probably do it like this:

    Code:
    import random
    
    rands = []
    for x in range(2):
        rands.append([random.randrange(1, 51) for y in range(6)])
    
    print rands
    that will leave you with a list of sets of 6 random numbers, though you can probably work something out if you want it in a different structure.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10
    Thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10
    I tried your script even though the programme prints 2 set of 6 random numbers they are not unique. ie [2,2,34,5,32,21,12] [1,3,6,7,9,32,7]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10
    I have sorted the above problem and here is my code

    Code:
    import sys
        import random
        it=0
        ans=int(raw_input(" Enter Number of Set(s): "))
        print        
        while it !=ans:
         guess=[]
         while len(guess) !=6:#for x in range(6):
            temp=random.randrange(1,50)
            while temp in guess:
                temp=random.randrange(1,50)
            guess+=[temp]
         guess.sort(cmp)
         for y in guess:
            print "%3d" %(y),
         it+=1
         print
    Luckyboy
    Last edited by luckyboy; April 26th, 2004 at 07:32 AM.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Heres my version although i cant test it right now so this may not work in any case you should have no problems fixing the examples if they are broken .. we'll see.

    Code:
    #!/usr/bin/env python
    
    form random import sample
    
    def randomNumbers():
        numbers = []
        for each in xrange(2):
            numbers.append(sample(xrange(1, 50), 6))
        return numbers
    or

    Code:
    #!/usr/bin/env python
    
    form random import sample
    
    numbers = [sample(xrange(1, 50), 6) for each in range(2)]
    This uses random.sample() to select 6 numbers from the xrange(1, 50) - the advantage of this is that every item should be unique. Although since this is being called twice it is possable that both lists may contain the same element. If this is a problem you could just do this...

    Code:
    #!/usr/bin/env python
    
    form random import sample
    
    def randomNumbers():
        numbers = sample(xrange(1, 50), 12)
        return (numbers[:6], numbers[7:])
    Which will return a tuple of unique random numbers (unsorted) - if you do want these sorted then simply call sort() on the numbers list.

    Hope this helps.

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10

    loops2


    Thank you all

IMN logo majestic logo threadwatch logo seochat tools logo