|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Yes i mean 2 sets of 6 random numbers
|
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
Thanks
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
Last edited by luckyboy : April 26th, 2004 at 07: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
|
|||
|
|||
|
loops2
Thank you all
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > python loop2 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|