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 Measure the Elapsed Time in Python
Python Set difference_update()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find Armstrong Number in an Interval
Python hash()
Python Functions
Python List insert()
Python Program to Merge Mails
Python Set isdisjoint()
Python property()
Python Program to Make a Flattened List from Nested List
Python Dictionary fromkeys()
Python vars()
Python Program to Capitalize the First Character of a String
Python chr()
Python Program to Print the Fibonacci sequence
Python Dictionary get()
Python Keywords and Identifiers
Python Dictionary
Python Program to Find Numbers Divisible by Another Number
Python String isalpha()
Python List remove()
Python exec()
Python Program to Find Hash of File
Python tuple()
Python String lstrip()
Machine Learning with Python for everyone - Mark E.Fenner
Python String casefold()
Python Program to Find the Size (Resolution) of a Image
Python Iterators
Python all()
Python Dictionary popitem()