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
1 2 3 4 5 6 | # Program to generate a random number between 0 and 9 # importing the random module import random print (random.randint( 0 , 9 )) |
Output
1 | 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:
1 | 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 Set update()
Python Shallow Copy and Deep Copy
Python hex()
Python String endswith()
Python getattr()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Convert Decimal to Binary Using Recursion
Python List append()
Python String rstrip()
Python String splitlines()
Python Program to Get the File Name From the File Path
Python String index()
Python String rfind()
Python range()
Python List sort()
Python bytes()
Python Program to Represent enum
Python datetime
Python Set pop()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python strptime()
Python pow()
Python open()
Python pass statement
Python Program to Check If Two Strings are Anagram
Python Program to Convert Kilometers to Miles
Python String rsplit()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Dictionary setdefault()
Python Program to Display the multiplication Table
Python Program to Find the Size (Resolution) of a Image
Python memoryview()