Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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, 10:15 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: 1 h 6 m
Reputation Power: 10
Pyhton Loop

Hi
I am new to python infact programming. I am trying to write a programme which will pick 6 random numbers between 1 to 50 but no duplicate numbers and not using append function.

Here is something i wrote

import sys
import random
array=[]
#array=[55,65,1,89,10,54,1,258,2]
for i in range(5):

# storing Random numbers to an array
temp= random.randrange(1,50)
array+=[temp]
#print array

# random number(1-49) Validation (pick 6 different numbers) i #am stuck here
if temp == array[i]:
temp =random.randrange(1,50)
array+=[temp]

#Bubble sort(Ascending order)
for y in range(len(array)-1,0,-1):
for x in range(y):
if array[x]> array[x+1]:
array[x],array[x+1]=array[x+1],array[x]

# Displaying Random numbers in order (working)
for v in array:
print v,



Can someone help
Thanks
Luckyboy

Reply With Quote
  #2  
Old April 23rd, 2004, 10:22 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: 1 h 6 m
Reputation Power: 10
Quote:
Originally Posted by luckyboy
Hi
I am new to python infact programming. I am trying to write a programme which will pick 6 random numbers between 1 to 50 but no duplicate numbers and not using append function.

Here is something i wrote

import sys
import random
array=[]
#array=[55,65,1,89,10,54,1,258,2]
for i in range(5):

# storing Random numbers to an array
temp= random.randrange(1,50)
array+=[temp]
#print array

# random number(1-49) Validation (pick 6 different numbers) i #am stuck here
if temp == array[i]:
temp =random.randrange(1,50)
array+=[temp]

#Bubble sort(Ascending order)
for y in range(len(array)-1,0,-1):
for x in range(y):
if array[x]> array[x+1]:
array[x],array[x+1]=array[x+1],array[x]

# Displaying Random numbers in order (working)
for v in array:
print v,



Can someone help
Thanks
Luckyboy

Reply With Quote
  #3  
Old April 23rd, 2004, 01:50 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Here's how I would do it, though I don't understand the restriction on not using .append()..

Code:
import random

picked_numbers = []

for i in range(6):
    newnum = random.randrange(1,50)
    while newnum in picked_numbers:
        newnum = random.randrange(1,50)
    picked_numbers += [newnum]

for num in picked_numbers:
    print num,


Normally instead of
Code:
picked_numbers += [newnum]
you'd just do
Code:
picked_numbers.append(newnum)
as it's more efficient.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #4  
Old April 23rd, 2004, 05:38 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: 1 h 6 m
Reputation Power: 10
Python loop

Hi it's working
Thank you strike

Reply With Quote
  #5  
Old April 23rd, 2004, 06:39 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
This problem is discussed at length in the book 'Programming Pearls' by Jon Bentley, which I recommend that anyone who is serious about programming should read.

Here is an alternative way. Create a list with the numbers 1-50 in it, shuffle it, and gets the first six elements

Code:
>>> import random
>>> numbers = range(1, 51)
>>> random.shuffle(numbers)
>>> numbers[:6]
[48, 16, 4, 40, 35, 9]


This is fine for a small list of 50, but if you wanted a random number from the set of 1..1,000,000,000, then it would create an array with a billion elements, and throw away a billion minus six of them. In that case I would go to the book I mentioned and use an alternative algorithm.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Pyhton Loop

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap