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 String rsplit()
Python Program to Extract Extension From the File Name
Python round()
Python Program to Get Line Count of a File
Python Program to Convert Celsius To Fahrenheit
Python oct()
Python String endswith()
Python bool()
Python object()
Python Program to Get the Full Path of the Current Working Directory
Python Program to Reverse a Number
Python strftime()
Python Modules
Python @property decorator
Python List insert()
Python Program to Make a Flattened List from Nested List
Python Program to Sort Words in Alphabetic Order
Python property()
Python print()
Python Exception Handling Using try, except and finally statement
Python Closures
Python Program to Split a List Into Evenly Sized Chunks
Python Shallow Copy and Deep Copy
Python String translate()
Python Program to Find the Largest Among Three Numbers
Python Tuple
Python hash()
Python Program to Sort a Dictionary by Value
Python Program to Count the Number of Occurrence of a Character in String
How to get current date and time in Python?
Python Machine Learning Eqution Reference - Sebastian Raschka
Python delattr()