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 String expandtabs()
Python Dictionary items()
Python Program to Print the Fibonacci sequence
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python next()
Python Dictionary get()
Python Set difference_update()
Python File I/O Operation
Python String isdecimal()
Python Dictionary fromkeys()
Python time Module
Python Dictionary copy()
Python max()
Python range()
Python List index()
Python Dictionary keys()
Python String join()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String casefold()
Python Program to Print Output Without a Newline
Python sorted()
Python Global, Local and Nonlocal variables
Python type()
Python datetime
Python Program to Remove Duplicate Element From a List
Python getattr()
Python Program to Find Sum of Natural Numbers Using Recursion
Deep Learning in Python - LazyProgrammer
Python Program to Count the Number of Each Vowel
Python slice()
Python List clear()
Python Program to Find Numbers Divisible by Another Number