Seed () and random numbers in Python

Possible duplicate:
Random Python: what happens if I don't use seed (someValue)?

Today I was just told about the seed() function from a programmer who is much more experienced than me. I usually call choice() with a list as an argument, since I no longer need random number functions.

My programming friend told me that calling a seed is necessary, because otherwise Python always starts random operations with a null value as the default seed. This means that although the numbers seem random, we do get the same sequence every time.

This seems rather strange to me. Can the choice() function, in fact, not call the seed before it does its thing? Or is this the reason why he cannot programmatically change his seed, because this in itself involves choosing a random number and, obviously, this is a bit of a problem if our ultimate goal is also choosing a random number!

I am writing a little here, but I am wondering if anyone has a clear idea of ​​how all this was implemented.

+6
source share
1 answer

Your friend is wrong, and he would know this if he read the documentation for the seed() function:

Initialize the random number generator. The optional argument x can be any hashed object. If x is omitted or None, the current system time is used; The current system time is also used to initialize the generator the first time the module is imported. If sources of randomness are provided by the operating system, they are used instead of system time (see os.urandom () for details on availability).

(Emphasize mine.)

He guesses, based on his knowledge of how he works in other languages. The seed() function is basically provided so that you can get a reproducible stream of pseudo random numbers (which is necessary for some specific applications).

The functions that you call directly from the random module are actually aliases of the methods of the hidden instance of the random.Random class. Each instance, at least in essence, calls seed() inside its __init__() .

The choice() function obviously does not call seed() before the operation, because it will mean re-sowing before each choice that defeats the sowing goal.

+28
source

Source: https://habr.com/ru/post/903335/


All Articles