In this example, you will learn to generate a random number in Python.
To understand this example, you should have the knowledge of the following Python programming topics:
To generate random number in Python, randint()
function is used. This function is defined in random module.
Source Code
# Program to generate a random number between 0 and 9 # importing the random module import random print(random.randint(0,9))
Output
5
Note that we may get different output because this program generates random number in range 0 and 9. The syntax of this function is:
random.randint(a,b)
This returns a number N in the inclusive range [a,b]
, meaning a <= N <= b
, where the endpoints are included in the range.
Related posts:
Python Program to Return Multiple Values From a Function
Python Program to Illustrate Different Set Operations
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Get a Substring of a String
Python String format()
Python String replace()
Python Program to Count the Number of Occurrence of a Character in String
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Access Index of a List Using for Loop
Python Program to Make a Simple Calculator
Python Program to Display Fibonacci Sequence Using Recursion
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python format()
Python classmethod()
Python repr()
Python Set intersection_update()
Python max()
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Count the Number of Each Vowel
Python Program to Reverse a Number
Python Anonymous / Lambda Function
Python pow()
Python Program to Find Armstrong Number in an Interval
Python Dictionary
Python List Comprehension
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Find Hash of File
Python Program to Catch Multiple Exceptions in One Line
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Node.js vs Python for Backend Development
Python Program to Get the Full Path of the Current Working Directory
Python ascii()