0

Got this weird error can anyone help?

Traceback (most recent call last):
  File "./test.py", line 172, in <module>
    main()
  File "./test.py", line 150, in main
    if random() < .5 and losttwice < 5:
TypeError: 'module' object is not callable


import urllib2,urllib,os,simplejson, random
Mark
  • 513
  • 2
  • 8
  • 19
  • Technically that link refers to `socket`, but it's the same cause. You could replace the word `socket` with random and they would be the same answers. – thegrinner Jul 03 '13 at 21:00

3 Answers3

5

You should use random.random() not just random. random is a module that contains functions like random, randint etc:

>>> import random
>>> random.random()
0.376462621569017

help on random.random:

random(...)
    random() -> x in the interval [0, 1).

If you only want to use the random() function from `random`` module, then you can also do:

>>> from random import random  #imports only random() from random module
>>> random()                   #now use random() directly,
0.7979255998231091
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • I'm using this if random() < .5 as 50% chance wil random.random() will offer the same function? – Mark Jul 03 '13 at 20:56
  • `help(random.random)`. yes. – Karoly Horvath Jul 03 '13 at 20:57
  • @Mark Glad that helped. :) Feel free to [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) when the system allows you. – Ashwini Chaudhary Jul 03 '13 at 21:04
  • In 2022 the [docs](https://docs.python.org/3.9/library/random.html#examples) clearly say call it like `random()` and still I had to search for this answer, which solves the problem. it doesn't properly show how to import anywhere. – Mote Zart Aug 19 '22 at 20:33
3

random is the name of a module; random.random is a function in that module. So you want to do random.random() < .5, not random() < .5.

mipadi
  • 398,885
  • 90
  • 523
  • 479
2

You're trying to call the random module. Try calling one of the functions in it instead, such as random.choice().

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358