How do I seed the random number generator for reproducibility?

To ensure that your random number generation produces the same results every time you run your code, you need to seed the random number generator. In Python, this can be done using the `random.seed()` function. This function initializes the random number generator with a specific integer value, so the sequence of random numbers generated is reproducible.

import random # Seed the random number generator random.seed(42) # Replace 42 with any integer for different sequences # Generate random numbers print(random.random()) # This will produce the same result every time print(random.randint(1, 10)) # Random integer between 1 and 10

random number generation seed random number generator reproducibility Python random