Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 23rd, 2004, 06:33 PM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old April 23rd, 2004, 06:57 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,221 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 22 h 21 m 4 sec
Reputation Power: 263
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

Reply With Quote
  #3  
Old April 24th, 2004, 05:27 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
Yes i mean 2 sets of 6 random numbers

Reply With Quote
  #4  
Old April 24th, 2004, 05:42 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
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.

Reply With Quote
  #5  
Old April 24th, 2004, 06:40 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
Thanks

Reply With Quote
  #6  
Old April 24th, 2004, 08:01 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
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]

Reply With Quote
  #7  
Old April 26th, 2004, 07:07 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
Quote:
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.

Reply With Quote
  #8  
Old April 26th, 2004, 09:58 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #9  
Old April 27th, 2004, 03:16 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
loops2

Thank you all

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > python loop2


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT