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 Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python globals()
Python bytes()
Python String expandtabs()
Python Inheritance
Python Program to Return Multiple Values From a Function
Python Anonymous / Lambda Function
Python String isnumeric()
Python repr()
Python hex()
Python Program to Split a List Into Evenly Sized Chunks
Python String isprintable()
Python slice()
Python String title()
Python Program to Find Factorial of Number Using Recursion
Python Program to Print the Fibonacci sequence
Python Shallow Copy and Deep Copy
Python Data Types
Python Program to Display Powers of 2 Using Anonymous Function
Python @property decorator
Python Program to Safely Create a Nested Directory
Python Program to Concatenate Two Lists
Python String isdecimal()
Python compile()
Python Program to Randomly Select an Element From the List
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Print Colored Text to the Terminal
Python Custom Exceptions
Python Function Arguments
Python String rjust()
Python Functions
Python String format()