Random Numbers#

In data science contexts random numbers are important for simulating data and for selecting random subsets of data. NumPy provides a submodule random for creating arrays of (pseudo-)random numbers.

import numpy as np

Random Number Generators#

NumPy supports several different algorithms for generating random numbers. For our purposes choice of algorithm does not matter (for crypto applications it matters!). Luckily NumPy provides a default one.

We first have to create a random number generator object (or get the default one) and initialize it with a seed. The seed determines the sequence of generated random numbers. Using a fixed seed is important if we need reproducable results (when testing things, for instance).

rng = np.random.default_rng(123)    # use some integer as seed here

Getting Random Numbers#

Random numbers may follow different distributions. NumPy provides many standard distributions, see Random Generator in NumPy’s documentation.

# random integers (arguments: first, last + 1, shape)
a = rng.integers(23, 42, (4, 10))

print(a)
[[23 35 34 24 40 27 27 26 29 26]
 [29 38 31 40 31 28 37 38 39 39]
 [23 32 28 27 27 38 38 27 30 37]
 [25 34 31 40 37 27 38 38 27 32]]
# uniformly distributed floats in [0, 1)
a = rng.random((4, 4))

print(a)
[[0.23155562 0.16590399 0.49778897 0.58272464]
 [0.18433799 0.01489492 0.47113323 0.72824333]
 [0.91860049 0.62553401 0.91712257 0.86469025]
 [0.21814287 0.86612743 0.73075194 0.27786529]]
# permutation of an array
a = np.array([1, 2, 3, 4 ,5])
b = rng.permutation(a)

print(b)
[4 2 5 3 1]