More advanced functions in python's random library
3 min read

More advanced functions in python's random library

In one of my last posts, I went over some basic functions in the random library. This time, I wanted to go over some less-known functions in the same library. Let's start:
Suppose, if every piece of code, the random library has already been imported.

random.seed()

This function, although less known, is probably still the most used one in this list. This function is used to set a state for the random number generator. Python's random library is actually pseudo-random. This means that the returned values can be predicted, if we use the same seed for two same random processes, we will get the same results. Here is an example:

print("first run:")
random.seed(0)
print(random.random())
print(random.random())

print("second run:")
random.seed(0)
print(random.random())
print(random.random())

Output:

first run:
0.8444218515250481
0.7579544029403025
second run:
0.8444218515250481
0.7579544029403025

As we can see, we get the same results if we set the seed as the same value. This function synergizes with other pseudo-random functions:

x = list(range(10000))
print("first run:")
random.seed(0)
print(random.randrange(2000,3000))
print(random.choice(x))

print("second run:")
random.seed(0)
print(random.randrange(2000, 3000))
print(random.choice(x))

Output:

first run:
2864
6311
second run:
2864
6311

random.getstate()/random.setstate()

Those two functions are used together and are similar to the previous random.seed() function, however, they let you store the state and reuse it later. Here is an example:

state = random.getstate()
print(random.random())
print(random.random())
print("after resetting the state:")
random.setstate(state)
print(random.random())
print(random.random())

Output:

0.3678899629955604
0.09259531746179073
after resetting the state:
0.3678899629955604
0.09259531746179073

random.gauss()

This function generates a random number following the gaussian distribution, it takes two arguments: the mean and standard deviation. Let's show an example by running the functions a couple of times and counting the number of occurrences of each value, the result will be the famous bell curve:

import matplotlib.pyplot as plt
x = list(range(100))
numbers = [int(random.gauss(50, 15)) for i in range(10000)]

y = [numbers.count(i) for i in x]
plt.plot(x,y)
plt.show()

Result:

Let's change the values a bit to show how the function adapts:

import matplotlib.pyplot as plt
x = list(range(50))
numbers = [int(random.gauss(10, 3)) for i in range(10000)]

y = [numbers.count(i) for i in x]
plt.plot(x,y)
plt.show()

Here, we can observe a peak around 10 with a standard deviation of 3

random.uniform()

This function is very similar to the random.randint() function, but instead it returns a floating value between two arguments: min and max.

for i in range(10):
    print(random.uniform(15, 47))

Output:

31.476495426782137
46.73463095172419
21.314530202373206
18.145596722218986
36.679519857729375
26.218381726040523
19.099600604343753
33.63606283945913
42.09694984381719
21.800395379104877

random.sample()

Finally, this function takes a given amount of samples from a list:

random.sample(population, k)

population is the list to sample and k is the number of samples needed. This function returns a list.

x = list(range(10))
print(random.sample(x, k=10))

Output:

[2, 8, 0, 4, 3, 6, 5, 9, 7, 1]

Another possibility is to add weight to each value to give bias:

x = list(range(10))

print("without weights:")
print(random.sample(x, k=10))

print("with weights")
print(random.sample(x, counts=[1,10,1,1,1,5,1,1,1,1], k=10))

Output:

without weights:
[3, 8, 5, 4, 2, 7, 1, 6, 9, 0]
with weights
[3, 1, 8, 4, 6, 1, 5, 1, 1, 1]

Thanks a lot for reading, consider subscribing to my newsletter, it's free and keeps me motivated!