Editor settings

General settings

Save settings

Learn to program with Python

Random Number Generation #

We can generate random numbers in Python by calling functions in the random module.

A module is a group of functions, variables, or objects organized together for ease of use. We can import a module using the import command followed by the name of the 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))
Once we have imported a module, we can use its functions or variables without importing it again.

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.

Output will be displayed here