Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

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

Updated on 21 December, 2023

7K+ views
7 min read

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.

Source

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: 

choice()

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

random()

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

shuffle()

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. 

Source

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

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: ‘)

print(random.choice([1,2,3,4,5,6]))

print(‘\r’)

# using the random() function

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

print(random.random())

print(‘\r’)

# 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=’’ ‘’) 

print(‘\r’)

random.shuffle(li)

# 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=’ ‘) 

print(‘\r’)

#using the randint() function

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

print(random.randint(1,10))

print(‘\r’)

#using the randrange() function

n=10

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

For i in range(1,n):

print(random.randint(0,100,2))

print(‘\r’)

Learn about: Python Developer Salary in India

Check out All Python tutorial concepts Explained with Examples.

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 )

Frequently Asked Questions (FAQs)

1. What are the most common errors made while writing Python code?

Some of the common errors made while writing Python code are:-
1. The most frequent cause of an error in a Python program is when a statement is not used correctly. A syntax error is a kind of mistake like this. The Python interpreter notifies you right away in such a case.
2. Most people make the mistake of not including colons after the loop syntax.
3. A common logical mistake is using two variables in the same application.
4. When an error happens at the runtime after passing the syntax test, it is referred to as an exception or logical type. When we divide any integer by zero, we get the ZeroDivisionError exception, and when we import a module that doesn't exist, we get the ImportError alert.

2. What is the best program to learn Python?

Jupyter Notebook is a useful tool for learning Python. Users can use Jupyter Notebook to combine data, code, and text to create an interactive computational story. Also, data scientists can use it to streamline end-to-end data science operations. Jupyter Notebook enables you to share Python code in real time with others. On the same page, you can view both the code and the results. It's useful to refer to other people's work as a learning resource. You may execute the code cell by cell to gain a better grasp of what it does. You can get started with the learning process by installing Jupyter Notebook using the Python pip command.

3. What are some other use cases to generate random numbers in Python?

One of the simplest and most common ways to generate random numbers in Python is to write a code that contains a random set of numbers such as 7,4,12,18, or any other similar set. There are, however, additional options that can provide the desired outcome. There are several computer ways for generating random numbers. You can create code to simulate the rolling of dice, coin flipping, and shuffling of playing cards to generate a random number. Gambling, statistical sampling, computer simulation, cryptography, totally randomized design, and other areas where creating an unpredictable outcome is desirable use such codes for making the work easier.