We use to generate a random number in our project for a sample data, which later can be used for testing, filled empty columns or for many other purposes, the key thing is we need to generate random data. In python, there are numerous ways to generate random data and we're going to explore some of them here in this article −
Python random() module
One of the important library, what comes with python is random and we are going to use it throughout in our code.
To use this module in your code, you just need to import it, that’s it and we are ready to use it.
import random
Let’s see how to use it −
import random print("What i will get, no idea as i'm using random.random()") print(random.random())
Output
What i will get, no idea as i'm using random.random() 0.5306626723173611
The second time, if I try to run the same program- you’ll get different output −
What i will get, no idea as i'm using random.random() 0.5504289430397661
Few points on the random module:
- random() is the basic function of the random module
- Almost all functions of the random module use random() function.
- Random() function will generate any number between [0.0 to 1.0).
Generate random integer number in Python
Below two functions we’re using to generate random integers −
- randint()
- randrange()
from random import randint, randrange print("Printing random integer ", randint(0, 20)) print("Printing random integer ", randrange(0, 20, 2))
Output
Printing random integer 15 Printing random integer 4
Randomly select an item from a list
Consider we have a list of companies name and we want to retrieve an item(company name) from that list. We can achieve this by,
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting company from a list: ', random.choice(companies))
Output
Randomly selecting company from a list: INFY
Randomly selecting multiple items from a list
Consider the above example but instead of one item(company) we want to select multiple items(companies) randomly from a list, we can achieve this through, random.sample() function −
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting 3 companies from a list: ', random.sample(companies,3))
Output
Randomly selecting 3 companies from a list: ['TCS', 'RELIANCE', 'INFY']
However, if we try to select items more than the number of items in the list, we encounter ValueError −
Input −
random.sample(companies,20)
output −
ValueError: Sample larger than population or is negative
Another method to select multiple random items from a list is – random.choices().
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting 3 companies from a list: ', random.choices(companies,k=6))
Output
Randomly selecting 3 companies from a list: ['TCS', 'TCS', 'INFY', 'HDFC', 'INFY', 'TCS']
As we can see from the above output, we may get a duplicate item from the list on using random.choices() method.
pseudo-random number generator in python
Pseudo-random number generator work by performing some operation on a value. Generally, this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.
import random print("Seed value 10: ") # Initialize seed value random.seed(10) for i in range(5): print(random.randint(1,100)) print() print("Seed Value 5: ") # this time we'll get different values random.seed(5) for i in range(5): print(random.randint(1,100)) print() print("Seed value: 10") # will get the same result, what we got initially random.seed(10) for i in range(5): print(random.randint(1,100))
Output
Seed value 10: 74 5 55 62 74 Seed Value 5: 80 33 95 46 89 Seed value: 10 74 5 55 62 74
As we see from the above example, if the seed is the same, it generates the first previous value. Each seed value corresponds to a fixed sequence of generated value for a given random number generator.
Generate cryptographically secure random numbers in python
We can generate a cryptographically secure random number in python 3.x. If we have python 3.6 or above we can use the new secrets module and the function rand below for it. It will generate a random number below the specified value.
import secrets #generate 10 secure random numbers between 10 and 500 for x in range(0,10): secV =10+ secrets.randbelow(500) print(secV)
Output
464 406 184 293 399 332 495 292 118 134
Another way for python 3.5 or below, we can use the random module and SystemRandom class to generate cryptographically secure random numbers is,
import random randGen = random.SystemRandom() for x in range(0,10): secV = 10+ randGen.randint(0,499) print(secV)
Output
374 211 425 264 217 97 210 39 319 52
Another way is to use the random and secrets(to secure data) module.
import secrets import random secNum = random.SystemRandom().random() print("secure number is ", secNum) print("Secure byte token", secrets.token_bytes(16))
Output
secure number is 0.5205307353786663 Secure byte token b'\x05T>\xacsqn0\x08\xc4\xf4\x8aU\x13\x9f\xcf'