Today, I was just told about the seed() function from a programmer much more experienced than me. I normally just call choice() with a list as an argument, as I don’t need anymore random number functionality than that.
My programmer friend told me that calling seed is necessary because otherwise Python always begins random number operations with zero as the default seed. This means that although the numbers appear random, we are really getting the same sequence every time.
This strikes me as rather odd. Does the choice() function, for example, really not call seed before it does its thing? Or is the reason it can’t programmatically change its seed because that in of itself would involve picking a random number, and obviously that it is a bit of a problem if our end goal is also to pick a random number!
I’m ranting a bit here, but I’m wondering if anyone has a clear idea of how all this was implemented.
Your friend is dead wrong, and would know so if he read the documentation for the
seed()function:(Emphasis mine.)
He’s guessing based on his knowledge of how it works in other languages. The
seed()function is mainly provided so that you can get a reproducible stream of pseudorandom numbers (which is necessary for some specific applications).The functions you call directly from the
randommodule are actually aliases to methods of a hidden instance of therandom.Randomclass. Each instance, at least in effect, callsseed()within its__init__().The
choice()function obviously does not callseed()before operation, because that would mean re-seeding before every choice, which defeats the purpose of seeding.