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 December 3rd, 2004, 09:52 PM
Noah_C Noah_C is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 34 Noah_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 5
Random Problem...

Whenever I try to call random.random or random.choice I get the following...

TypeError: 'module' object is not callable

or

AttributeError: 'module' object has no attribute 'choice'

both of these come from previously working or examples I found on the web. Do I need to re-install python ?

Reply With Quote
  #2  
Old December 3rd, 2004, 11:08 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
the function is random.randrange, random.random doesnt exist
if you want to use random.choice you need to give a group of elements to choose from to the frogram
bye

Reply With Quote
  #3  
Old December 3rd, 2004, 11:59 PM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Hi,
Find below some of the tools belonging to the random module... can't figure out why u r getting such an error! But u cld try the following examples...n report if the problem still persists....what say?

Code:
>>> import random
>>> for number in range(10):
... 	x=random.random()
... 	print x
... 	
0.435063217885
0.992538870884
0.653449999022
0.137293325169
0.12339139281
0.632875520517
0.835285602794
0.732443595278
0.353933184167
0.953251297425
>>> toons=['Dexter','Bubbles','DeeDee','ButterCup']
>>> random.choice(toons)
'Bubbles'
>>> random.randrange(10)
1
>>> random.randrange(10)
3
>>> random.sample(range(40),5)
[25, 38, 15, 29, 18]
>>> 


All the abv are self explanatory...cld shed some light on random.sample...
it will give you an unique list in the range provided..

Subha

Reply With Quote
  #4  
Old December 4th, 2004, 07:54 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 10 m 32 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
Unless you need the extra control you can get from random.randrange() I would probably use random.randint(), though it makes no real difference which you choose .

Code:
>>> import random
>>> 
>>> random.randrange(0, 10)
4
>>> random.randrange(0, 10)
0
>>> random.randrange(0, 10)
8
>>> random.randrange(0, 10, 2)
8
>>> random.randrange(0, 10, 2)
0
>>> random.randrange(0, 10, 2)
2
>>> random.randint(0, 10)
10
>>> random.randint(0, 10)
2
>>> random.randint(0, 10)
4
>>> 


Note that the second set of calls to random.randrange() – the ones with the third argument – will only return even numbers from the range.

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


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Random Problem...


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 4 hosted by Hostway
Stay green...Green IT