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 reversed()
Python print()
Python any()
Python set()
Python Strings
Python Variables, Constants and Literals
Python Exception Handling Using try, except and finally statement
Python String rpartition()
Python Shallow Copy and Deep Copy
Python Dictionary items()
Python Program to Illustrate Different Set Operations
Python break and continue
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Represent enum
Python Program to Create a Countdown Timer
Python String capitalize()
Python Program to Catch Multiple Exceptions in One Line
Python Set isdisjoint()
Python String rfind()
Python Keywords and Identifiers
Python Program to Find ASCII Value of Character
Python Program to Calculate the Area of a Triangle
Python Set discard()
Python Program to Check the File Size
Python List Comprehension
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Function Arguments
Python String count()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Object Oriented Programming
Python Input, Output and Import
Python String isalpha()