Are you using the built-in functions of the random library but getting the “NameError: name ‘random’ is not defined” error?
Python has provided us with many built-in but helpful libraries that we can utilize to do specific tasks. One of these helpful libraries is random; it helps generate random numbers from a given range or choose a random element of an iterable like lists, tuples, strings, etc.
In this article, we’ll discuss what is the “NameError: name ‘random’ is not defined” error and how to fix it. For example, to utilize the built-in libraries of Python, you first need to install the required dependencies and then import them into your current program to work correctly.
Let’s first discuss what is random in Python and how it works.
Table of Contents
What is Random in Python?
Random is a built-in library in Python that allows you to generate pseudo-random numbers. It performs different operations like choosing a random number from a given iterable and shuffling it.
The following are the different modules of Random in Python that we can use to perform different mathematical operations:
Method | Description |
seed() | It is used to initialize a random number generator. |
getstate() | It returns the current internal state of the random number generator. |
setstate() | It restores the state of the random number generator to the specified state. |
getrandbits() | It returns a number that represents random bits. |
randrange() | It returns a random number within a defined range. |
randint() | It returns a random integer within a defined range. |
choice() | It returns a random element from a defined iterable like list, tuple, or string. |
choices() | It returns a list by randomly selecting elements from a defined iterable like a list, tuple, or string. |
shuffle() | It is used to shuffle the order of elements of a list, string, or tuple. |
Sample() | It returns a random sublist from a list. |
random() | It returns a float number between 0 and 1. |
uniform() | It returns a random float number between a defined range. |
triangular() | It returns a random float number between a given range, and you can also set a mode parameter to specify a midpoint between parameters. |
betavariate() | Depending on the beta distribution, it returns a random float number in the range of 0 and 1. |
expovariate() | It returns a random float number depending on the exponential distribution. |
gammavariate() | It returns a random float number depending on the gamma distribution. |
gauss() | It returns a random float number depending on the gaussian distribution. |
logonormvariate() | This function returns a random float number depending on the log-normal distribution. |
normalvariate() | It returns a random float number depending on the normal distribution. |
vonmisesvariate() | It returns a random float number depending on the von mises distribution. |
paretovariate() | It returns a random float number depending on the Pareto distribution. |
weibullvariate() | It returns a random float number depending on the Weibull distribution. |
What is the the “NameError: Name ‘Random’ Is Not Defined” Error in Python?
In Python, we face the NameError when a variable or a function is not defined but used somewhere in your python program. For example, the NameError name random is not defined in Python in a similar case. Let’s see an example and understand the cause of this error.
Code
# generate a random integer number between 1-10 n1 = random.randint(1, 10) print(n1)
Output
How to Fix the “NameError: Name ‘Random’ is Not Defined” Error in Python?
To fix the “NameError: name ‘random’ is not defined” error in Python, you must first install the random library into your machines and then import it into your current program.
To install the random library, we’ll pip command. And to install it, go to your Command Line Interface (CLI) and enter the following command:
Command
pip install random
This command will install the random library into your local machines now, and you can easily import it into your current Python program. Let’s see how it works after installing and importing the library.
Code
# importing library import random # generate random integer numbers between 1-10 n1 = random.randint(1, 10) n2 = random.randint(1, 10) print("The first random number is ", n1) print("The second random number is ", n2)
Output
The first random number is 3 The second random number is 7
Congrats! We have finally resolved the NameError. Now, we can use the numerous functions of the random library in our Python program.
Conclusion
To conclude the article on how to fix the “NameError: name ‘random’ is not defined” error in Python, we have discussed the random library and its numerous helpful functions of it. Furthermore, we have discussed the cause of these errors and how to fix them by installing and importing the random library.
Let’s have a quick recap of the topics discussed in the article.
- What is random in Python?
- What are the different functions of the random library in Python?
- What is the “NameError: name ‘random’ is not defined” error?
- How to install the random library in Python?
- How to fix the “NameError: name ‘random’ is not defined” error in Python?
Let’s do a quick exercise 💻, execute any of the above functions of the random library, and comment on the output.