Need some random numbers in your program? This article will help you to know different techniques to generate a random number in python. While developing small, large or enterprise application, It requires generating random numbers. Let’s deep dive to Generate Random number using Python.
Python provides ” random ” library to generate a random number using built un methods. Just “import random” and start generating a number which will be random.
Here is the example to generate random integer number.
Generate Random number using Python
# Program to print 5 random integer numbers between 1 to 10000 import random for x in range(5): print random.randint(1, 10000)
This program prints 5 random integer number between 1 to 10000. randint is an inbuilt function in python random library. It has two parameters.
randint(a,b) It generates the random integer number N such that.
a <= N <= b
# Program to print 5 random numbers between 0.0 to 1.0 for x in range(5): print random.random()
This random function returns the random floating point number in the range (0.0, 1.0).
For more information to generate random numbers, please visit the official python document for random.
To Learn more about Python, Please visit the Python category.