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

Join Date
Apr 2004
Posts
57
Rep Power
14

#### 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.

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 10:15 AM.
2. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
London, England
Posts
1,585
Rep Power
1376
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
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

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

Join Date
Jan 2004
Posts
84
Rep Power
14
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.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

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

Join Date
Apr 2004
Posts
57
Rep Power
14
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]
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2004
Posts
57
Rep Power
14
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 08:32 AM.
8. 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.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2004
Posts
57
Rep Power
14

#### loops2

Thank you all