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 Delete an Element From a Dictionary
Python sleep()
Python max()
Python Object Oriented Programming
Python Modules
Python Program to Display Calendar
Python List append()
Python String rfind()
Python String isdigit()
Python Statement, Indentation and Comments
Python Program to Get the File Name From the File Path
Python hash()
Python if...else Statement
Python Program to Split a List Into Evenly Sized Chunks
Java Program to Implement the Program Used in grep/egrep/fgrep
Python String endswith()
Python Program to Count the Occurrence of an Item in a List
Python Program to Add Two Matrices
Python Program to Parse a String to a Float or Int
Python String rsplit()
Python Dictionary values()
Python float()
Python Program to Find the Largest Among Three Numbers
Python List count()
Python Program to Convert Celsius To Fahrenheit
Python Operator Overloading
Python Machine Learning Eqution Reference - Sebastian Raschka
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String title()
Python oct()
Python Program to Compute all the Permutation of the String
Python Multiple Inheritance