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 Check if a Number is Odd or Even
Python Program to Add Two Numbers
Python String splitlines()
Python String encode()
Python Program to Catch Multiple Exceptions in One Line
Python callable()
Python object()
Python staticmethod()
Python Tuple
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Convert Kilometers to Miles
Python Operator Overloading
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python globals()
Python getattr()
Python Matrices and NumPy Arrays
Python while Loop
Python String rstrip()
Python Package
Python list()
Python Dictionary values()
Python Program to Get the Last Element of the List
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python List reverse()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String rjust()
Python Global, Local and Nonlocal variables
Python String join()
Python reversed()
Python Function Arguments
Python Program to Convert Two Lists Into a Dictionary
Python String count()