|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ? |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 ![]() |
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Random Problem... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|