Python provides a pseudo-random number generator in its standard library random
module. The module contains various functions that generate random numbers, both integers, and floating-point numbers. To ensure the reproducibility of the generated sequence, you can set a seed value using random.seed()
before generating the random numbers.
Here's an example of how to generate a sequence of 5 random integers between 0 and 9 using the random
module in Python:
main.py177 chars9 lines
Output:
main.py10 chars6 lines
In the above code, random.seed(1234)
sets the seed value to 1234, and random.randint(0, 9)
generates a random integer between 0 and 9 (inclusive). The for
loop is used to generate 5 such random integers.
By setting the same seed value every time, the sequence of the generated random integers will be the same. If you change the seed value, you will get a different sequence of random integers.
gistlibby LogSnag