How To Resolve NameError: Name ‘random’ Is Not Defined In Python

NameError: name 'random' is not defined in Python

To fix the NameError: name ‘random’ is not defined in Python, you can use the following: import the random module before using it or use the numpy.random module. Please read the following article for details.

What causes the NameError: name ‘random’ is not defined in Python?

Python provides a random module that is extremely easy to deal with random numbers. The Random module implements a pseudo-random number generator and contains functions that allow us to deal directly with randomness.

‘NameError’ is an error when you use a variable, function, or module that is not declared or you are not using it properly.

NameError: name ‘random’ is not defined happens because you don’t import the random module before using it.

Example:

# Use the random() function
result = random.random()
print(result)

Output:

Traceback (most recent call last):
  File "code.py", line 2, in <module>
    result = random.random()
NameError: name 'random' is not defined

How to solve the NameError: name ‘random’ is not defined in Python?

Import the “random” module

Example:

  • Import requests from random Python packages (this is an important step you need to remember as it is the cause of this error).
import random

# Use the random() function
result1 = random.random()
print('Random output from random() function:', result1)

# Use the randint() function
result2 = random.randint(0,5)
print('Random output from randin() function:', result2)
 
# Use the randrange() function
result3 = random.randrange(2,7,2)
print('Random output from randrange() function:', result3)

Output:

Random output from random() function: 0.6993824842072576
Random output from randin() function: 5
Random output from randrange() function: 6

The random.randint() method takes two arguments, a range from 0 to 5, and returns a random number from that range.

The random() method generates a random float in the range (0.0, 1.0).

The random.randint() method takes two arguments representing the range from which the method draws a random integer.

Import random from the numpy module

You can use the numpy.random module to solve this problem.

Example:

from numpy import random

# Use the numpy.randint() function
result1 = random.randint(2, size=10)
print('Random output from numpy.randint() function:', result1)

# Use the numpy.rand() function
result2 = random.rand(3,2)
print('Random output from numpy.rand() function:', result2)

# Use the numpy.random() function
result3 = random.random(3)
print('Random output from numpy.random() function:', result3)

Output:

Random output from numpy.randint() function: [0 0 1 0 1 0 1 0 0 0]
Random output from numpy.rand() function: 
[[0.74079175 0.13131399]
 [0.16306477 0.04369934]
 [0.530455 0.90342889]]
Random output from numpy.random() function: [0.9458757 0.14450404 0.3792123 ]

In the example above:

  • The function numpy.random.randint returns a numpy array of random integers from low (inclusive) to high (exclude). 
  • numpy.random.rand returns a numpy array of random values ​​in a given shape.
  • numpy.random.random returns a numpy array of random numbers in the range [0.0, 1.0).

Summary

I have shown the causes and given some solutions to fix the NameError: name ‘random’ is not defined in Python. This is a pretty simple bug in Python. I hope you can fix it as soon as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *