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 Calculate the Area of a Triangle
Python oct()
Python *args and **kwargs
Python Set pop()
Python timestamp to datetime and vice-versa
Python Dictionary values()
Python Numbers, Type Conversion and Mathematics
Python memoryview()
Python filter()
Python Iterators
Python bin()
Python Program to Compute all the Permutation of the String
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String isprintable()
Python Program to Print Hello world!
Machine Learning with Python for everyone - Mark E.Fenner
Python classmethod()
Python Program to Find Hash of File
Python Program to Find the Size (Resolution) of a Image
Python abs()
Python String rindex()
Python Program to Trim Whitespace From a String
Python String isalpha()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python if...else Statement
Python slice()
Python Set copy()
Python Program to Check Whether a String is Palindrome or Not
Python Program to Count the Occurrence of an Item in a List
Python Program to Parse a String to a Float or Int
Deep Learning with Python - Francois Cholletf
Python Errors and Built-in Exceptions