[ACCEPTED]-What is suggested seed value to use with random.seed()?-random

Accepted answer
Score: 14

According to the documentation for random.seed:

If x 23 is omitted or None, current system time is used; current 22 system time is also used to initialize the 21 generator when the module is first imported. If 20 randomness sources are provided by the operating 19 system, they are used instead of the system 18 time (see the os.urandom() function for details on availability).

If 17 you don't pass something to seed, it will 16 try to use operating-system provided randomness 15 sources instead of the time, which is always a better bet. This 14 saves you a bit of work, and is about as 13 good as it's going to get. Regarding availability, the 12 docs for os.urandom tell us:

On a UNIX-like system 11 this will query /dev/urandom, and on Windows 10 it will use CryptGenRandom.

Cross-platform 9 random seeds are the big win here; you can 8 safely omit a seed and trust that it will 7 be random enough on almost every platform you'll use 6 Python on. Even if Python falls back to 5 the time, there's probably only a millisecond 4 window (or less) to guess the seed. I don't 3 think you'll run into any trouble using 2 the current time anyway -- even then, it's 1 only a fallback.

Score: 5

For most cases using current time is good 3 enough. Occasionally you need to use a fixed 2 number to generate pseudo random numbers 1 for comparison purposes.

Score: 3

Setting the seed is for repeatability, not 3 security. If anything, you make the system 2 less secure by having a fixed seed than one 1 that is constantly changing.

Score: 1

Perhaps it is not a problem in your case, but 5 ont problem with using the system time as 4 the seed is that someone who knows roughly 3 when your system was started may be able 2 to guess your seed (by trial) after seeing 1 a few numbers from the sequence.
eg, don't use system time as the seed for your online poker game

Score: 0

If you are using random for generating test 10 data I would like to suggest that reproducibility 9 can be important.

Just think to an use case: for 8 data set X you get some weird behaviour 7 (eg crash). Turns out that data set X shows 6 some feature that was not so apparent from 5 the other data sets Y and Z and uncovers 4 a bug which had escapend your test suites. Now 3 knowing the seed is useful so that you can 2 precisely reproduce the bug and you can 1 fix it.

More Related questions