#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    11

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    10
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    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
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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


IMN logo majestic logo threadwatch logo seochat tools logo