Events and Probability Spaces · Process Tree
Lesson 4
The code above contains the line \(\texttt{seed(17)}\). It is used to initialize the pseudo-random number generator in Python. As the name suggests, the \(\texttt{random()}\) method generates not truly random, but pseudo-random numbers: they resemble random numbers but are not truly random because they are generated by a deterministic algorithm. However, this code has the property of reproducibility: two runs of this code will yield identical results. This property is useful, especially for debugging code that uses pseudo-random numbers. To obtain different pseudo-random numbers for different runs, you can either remove initialization from the code or initialize it, for example, with the current time (\(\texttt{seed(time())}\)).
from random import randint, seed
seed(17)
print([randint(1, 10) for _ in range(10)])
seed(17)
print([randint(1, 10) for _ in range(10)])
seed(23)
print([randint(1, 10) for _ in range(10)])[9, 7, 5, 6, 5, 3, 9, 5, 2, 1]
[9, 7, 5, 6, 5, 3, 9, 5, 2, 1]
[5, 2, 1, 10, 5, 7, 7, 9, 6, 3]