How to Generate Random Number in Python [Code with Use Case Examples]

Introduction to Python 

Python is one of the easiest languages to start learning a program. Python is a general-purpose programming language and designed to increase code readability. It follows an object-oriented approach and enables programmers to write logical and clear codes for small as well as large scale projects. It contains a comprehensive library set that provides many in-built functions for programmers to implement, which makes it easier to use and learn. 

Let us now move on to know what is random number generation and how to generate random numbers in Python.

What is Random Number Generation?

Random number generation is the process of producing random numbers whenever required. These numbers can be generated with or without giving conditions. For example, setting the range of numbers from 1 to 100, asking for only even numbers, etc.

Random number generation is one of the most frequently used techniques in programming when we need to input a large number of entities. It is very time-consuming as well as a tedious process when it comes to entering a large number of inputs. Random number generation is also used in case of games; for example, ludo, where we need to generate numbers to roll a dice, or snake and ladder, etc. Some of the other important uses of random number generators are in Cryptography, Monte-Carlo-method simulations, and hash algorithms. If you are a beginner and interested to learn more about data science, check out our data science courses from top universities.

As we now know the importance and requirement of random number generation, let us now learn how to generate a random number in Python



Check out: Python Interview Questions & Answers

Code to Generate Random Number in Python

As we already know that Python is a general-purpose and an easy-to-learn programming language, let us look upon the codes that enable us to generate random numbers. Python is known for its collection of standard libraries and functions, and similarly, we also have a set of functions that can be used to generate random numbers. 

These functions are as follows: 


The choice() function is used to generate a random number from a built-in container of numbers. 


The random() function is used to generate a random number, which is of a floating-point number and ranges between 0 and 1. 


The shuffle() function is used to rearrange all the values present in the provided list randomly.

randint(beginning, end)

The randint() function takes two arguments, beginning, and end, that denote the starting and ending numbers. It produces random integer type numbers between the given starting and ending numbers. 

randrange(beginning, end, step)

The randrange() function has three arguments- beginning, end, and step, denoting the starting number, ending number, and the steps between numbers. It generates random numbers between the range of beginning and ending number input, in a periodic interval equal to the provided steps. 


The code for using all of these functions to generate random number in Python is: 

#importing the random library

import random

# using the choice() function

print(‘Generate random number in python using the choice() function: ‘)



# using the random() function

print(‘Generate random number in python using the choice() function: ‘)



# using the shuffle() function

# Initializing the list

li = [7, 4, 12, 3, 17]

 # Printing list before shuffling

print (‘The list that is given before shuffling is: ‘, end=’’)

for i in range(0, len(li)):

print (li[i], end=’’ ‘’) 



# Printing the sample list [7, 4, 12, 3, 17] after shuffling

print(‘Generate random number in python using the shuffle() function: ‘)

print (‘The list after shuffling using the shuffle() function is : ‘, end=’’) 

for i in range(0, len(li)):

print (li[i], end=’ ‘) 


#using the randint() function

print(‘Generate random number in python using the randint() function: ‘)



#using the randrange() function


print(‘Generate random number in python using the randrange() function: ‘)

For i in range(1,n):



Learn about: Python Developer Salary in India

Bottom line

The Python language has been one of the most demanded programming languages in recent times and is preferred by beginners as well as experienced programmers. Python is used for server-side for web-development, software development, mathematics, and also for system scripting. There are various interesting projects and articles on Python that you can find on our blog. Python is also majorly used for Data Science and Machine Learning.

If you are interested in Data Science and Machine Learning and are curious to learn it, you can visit upGrad’s courses made by industry experts as well as experienced academicians. These courses are made for working professionals who want to change their careers or advance in their respective fields. These are some of the courses that you can complete to know more about these technologies. 

Executive PG Program (NASSCOM certified Diploma course in Data Science developed by upGrad in coordination with IIITB)

Associate Certificate Program in Data Science (PG certification course developed by upGrad along with IIITB with hands-on industry projects, expert mentorship, and 24×7 support)

Executive PG Programme in Machine Learning and AI (PG Diploma course in Machine Learning and AI provided by IIITB and upGrad )

Masters of Science in Data Science (Masters course in Data Science provided by Liverpool John Moores University and upGrad)

Masters of Science in Machine Learning and AI (Master’s course in AI and Machine Learning provided by upGrad along with IIITB and Liverpool John Moores University )

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Data Science Course