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 Input, Output and Import
Deep Learning in Python - LazyProgrammer
Python oct()
Python issubclass()
Python bool()
Python help()
Python Program to Check if a Number is Odd or Even
Python Program to Calculate the Area of a Triangle
Python max()
Python getattr()
Python Program to Make a Simple Calculator
Python bytearray()
Python if...else Statement
Python Program to Get the Class Name of an Instance
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python memoryview()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python object()
Python reversed()
Python next()
Python String islower()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String split()
Python Function Arguments
Python Program to Print Colored Text to the Terminal
Python String rsplit()
How to Get Started With Python?
Python Package
Python Program to Sort a Dictionary by Value
Python property()
Python Program to Merge Two Dictionaries
Python String swapcase()