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 Display Calendar
Python Program to Display the multiplication Table
Python Errors and Built-in Exceptions
Python print()
Python bytearray()
Python Program to Check If Two Strings are Anagram
Python float()
Python String upper()
Python Program to Find the Largest Among Three Numbers
Python oct()
Python String format()
Python __import__()
Python Set discard()
Python String islower()
Python Program to Find the Factors of a Number
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Set add()
Python Program to Find Hash of File
Python Program to Measure the Elapsed Time in Python
Python Set intersection()
Python Program to Check If a List is Empty
Python Program to Check Whether a String is Palindrome or Not
Deep Learning in Python - LazyProgrammer
Python setattr()
Python Operators
Python Shallow Copy and Deep Copy
Python Dictionary clear()
Python isinstance()
Python set()
Python Set pop()
Python String translate()
Python Input, Output and Import