Random Number Generation #
We can generate random numbers in Python by calling functions in the random module.
We can access each function or variable in the module by using the . operator followed by the name of the variable. For example, in the random module, there is a function named randint() used to generate random integers. The randint() function takes two arguments: the starting number and the ending number that we want to generate. For example, the code below generates a random number between 1 and 100:
import random
print(random.randint(1, 100))
In addition to importing the entire module, we can also import specific functions or variables using the from import statement. The following code is equivalent to the previous example:
from random import randint
print(randint(1, 100))
In addition to the randint() function, the random module also has other functions that can be used. You can read more about the random module at this link.
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules