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 Get Line Count of a File
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python eval()
Python Dictionary items()
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Get the Full Path of the Current Working Directory
Python del Statement
Python Program to Get the Last Element of the List
Python Set remove()
Python enumerate()
Python List remove()
Python Program to Find the Factors of a Number
Python Program to Swap Two Variables
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Get a Substring of a String
Python Modules
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Set difference()
Python String isprintable()
Python Machine Learning - Sebastian Raschka
Python Tuple index()
Python Program to Solve Quadratic Equation
Python Object Oriented Programming
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python String zfill()
Python Input, Output and Import
Python __import__()
Python List append()
Python classmethod()
Python Dictionary
Python Tuple
Python String isupper()