How to generate random numbers in Python

How to generate random numbers or integers in Python. Write a python program to generate random numbers that take input as minimum or maximum and generate output as a random number between the range.

How to generate random integers?

Random integers can be generated using functions such as randrange() and randint().

randint(a,b):This function generates integers between a given limit. It takes two parameters where the first parameter specifies the lower limit and the second one specifies the upper limit.

randrange(): The randrange() function, as mentioned earlier, allows the user to generate values by stepping over the interval count.

What is a random integer?

Input: Random integer from 0 to 99

Output: Randomly generated integer is:  45

Algorithm to generate a random integer:

  •       Import Random.
  •       Assign a variable that will randomly generate a number between a certain range.
  •       Print that variable.

Python program to generate random integers.

Method 1: using randint()

import random
print("Random integer from 0 to 9")
num1 = random.randint(0, 9)
print("Randomly generated integer is: ", num1)

Output: Printing the randomly generated integers.

Method 2: using randrange()

import random
print("Randomly generated numbers using randrange:")
for x in range(5):

Output: Printing the randomly generated integers.