Setting a Random Seed
- 01:18
How to use the seed function in generating reproducible sets of random numbers in Python.
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays Python Random Seed Seed FunctionTranscript
You'll notice that if I execute the cell again, all of my numbers change. So every time that I execute that cell, I'm gonna get a new set of random integers. And that might be what you want. But what if you want to reproduce your results? What if you wanna be able to generate random numbers or use any of the other functions in the random module and you wanna reproduce your results or hold them static? Well, that's where the seed function comes in. It lets you set a random seed, which is like choosing a universe of random functions. So if I go above the code that I wrote here, and I write np random for the random module seed, and then inside those parentheses I choose a number, let's say 1, 2, 3. Now when I execute that cell, I'm gonna get a different set of random numbers, but if I execute it again, every time they're gonna stay the same.
So this can be useful if you're working with other people and you want to make sure that they're getting the same results as you are. So as we're working on machine learning, we're going to use some random functions and you wanna be able to get an answer that matches mine so that you can check your work. So one of the ways that we accomplish that is by setting a random seed.