Python Random Number Generator – Python Random Module
Ever wondered how you get a random dice number every time you play online Ludo. Or how shuffling your song playlist works?
Turns out, your gaming or music app generates random numbers underneath the hood. And based on this random number, it decides which dice number to display or which song to play.
But doesn’t “random” shuffling your playlist seems far from random? This is because these random numbers are not truly random, rather they are pseudo-random numbers. A pseudo-random number generator (PRNG) is an algorithm that generates seemingly random numbers. Let’s learn about Python Random Number Generator.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
Python Random Module
Python generates these pseudo-random numbers using the random module. But if you head to the Python docs for the documentation of this module, you’ll see a warning there –
“The pseudo-random generators of this module should not be used for security purposes.”
Apparently, the pseudo-random numbers that the random module generates are not cryptographically secure. Python uses Mersenne Twister algorithm for generating random numbers. But this algorithm is completely deterministic, making it an unsuitable choice for cryptographic purposes.
Now let’s move on to various built-in functions, under the random module. These functions are capable of generating pseudo-random numbers under different scenarios.
Built-in Functions for Python Random Number Generator
The list below contains a brief description of the functions we’re going to cover for random number generation.
|Function||What it does|
|randint(x, y)||Generates a random integer from x to y, including the x and y.|
|randrange(start, stop, step)||Generates a random integer in the range(start, stop, step)|
|random()||Generates a random floating-point number in the interval [0,1)|
|uniform(x, y)||It generates a floating-point value between x and y.|
|sample(population, k)||Selects k unique random elements from a population sequence or set.|
|choice(seq)||Chooses a random element from a non-empty sequence seq.|
|shuffle(x)||Shuffles list x in place.|
|seed(x)||Generates the same sequence of random numbers every time you call seed(x).|
Let’s now dive deeper into each of these functions.
1. Python randint()
This function generates an integer between the specified limits. It takes two arguments x and y and produces integer i such that x <= i <= y.
>>> import random >>> random.randint(3, 6)
2. Python randrange()
This function takes 2 arguments start and stop along with 1 optional argument step. And returns a random integer in the range(start, stop, step). The default value of step is 1.
>>> import random >>> random.randrange(1, 10, 2)
In this example, the output will be a random integer from [1, 3, 5, 7, 9] as the start and stop values are 1 and 10 respectively and the step value is 2.
3. Python random()
This function returns a random floating-point number between 0 and 1 (excluding 0 and 1).
>>> import random >>> random.random()
4. Python uniform()
This function lets you generate a floating-point number within a specific limit. It takes two arguments to start and stop and then returns a float between the start and stop (including the limits).
>>> import random >>> random.uniform(6, 9)
5. Python choice()
If you want to choose a random element from a specific sequence, you can use this function. It takes one argument – the sequence. And it returns a random element from the sequence.
>>> import random >>> seq = (12, 33, 67, 55, 78, 90, 34, 67, 88) >>> random.choice(seq)
6. Python sample()
If you want more than one random element from a sequence, you can use sample(). It takes two arguments population and k, where population is a sequence and k is an integer. Then it returns a list of k random elements from the sequence population.
>>> import random >>> seq = (12, 33, 67, 55, 78, 90, 34, 67, 88) >>> random.sample(seq, 5)
7. Python shuffle()
This function takes one argument – a list. It then shuffles the elements of the list in place and returns None.
>>> import random >>> l = [10, 20, 30, 40, 50] >>> random.shuffle(l) >>> print(l)
8. Python seed()
You can use this function when you need to generate the same sequence of random numbers multiple times. It takes one argument- the seed value. This value initializes a pseudo-random number generator. Now, whenever you call the seed() function with this seed value, it will produce the exact same sequence of random numbers.
import random # seed value = 3 random.seed(3) for i in range(3): print(random.random(), end = ' ') print('\n') # seed value = 8 random.seed(8) for i in range(3): print(random.random(), end = ' ') print('\n') # seed value again = 3 random.seed(3) for i in range(3): print(random.random(), end = ' ') print('\n')
0.23796462709189137 0.5442292252959519 0.369955166548079250.2267058593810488 0.9622950358343828 0.12633089865085956
0.23796462709189137 0.5442292252959519 0.36995516654807925
Notice that for seed = 3, the same sequence gets produced every time.
In this article, we looked at some majorly used functions which are capable of generating pseudo-random numbers in Python. We saw the functions we can use for generating integers. We also saw how to generate random floating-point values. Lastly, we saw how we can use built-in functions to select random elements out of a sequence. You can build various small projects using random numbers. You can build games, you can use random numbers while working with statistics in Data Science and much more. Use your creativity and play with numbers!
Hope you enjoyed the Python Random Number Generator tutorial!!