Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random.randint error

I have some code that looks something like this:

import random  n = 0 while n <= 50:   n = n+1   a = random.randint(1, 16)   b = random.randint(1, 5)   print n, ". ", a, "-", b, "= " 

For some reason, when running it, I get the following error: AttributeError: 'module' object has no attribute 'randint'. However, I have no problems when running the same random.randint queries in IDLE. How can I fix this?

like image 458
foobar Avatar asked Nov 14 '10 17:11

foobar


People also ask

How do I fix AttributeError module random has no attribute Randint?

The Python "AttributeError module 'random' has no attribute 'randint'" occurs when we have a local file named random.py and try to import from the random module. To solve the error, make sure to rename any local files named random.py . Here is an example of how the error occurs in a file called random.py .

What is random Randint in Python?

Python Random randint() Method The randint() method returns an integer number selected element from the specified range. Note: This method is an alias for randrange(start, stop+1) .

Is random Randint actually random?

We call randint a pseudo-‐random number generator (PRNG) since it generates numbers that appear random but are not truly random.

Does random Randint include 0?

randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]. Use a random. randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random.


1 Answers

You have another module called "random" somewhere. Did you name your script "random.py"?

like image 89
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 11:09

Ignacio Vazquez-Abrams