tags:

views:

342

answers:

5

Simple enough question:

I'm using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this default to the current time, but this is not ideal. It seems like a string literal constant (similar to a password) would also not be ideal/strong

Suggestions?

Thanks, -aj


UPDATE:

The reason I am generating random integers is for generation of test data. The numbers do not need to be reproducable.

+7  A: 

According to the documentation for random.seed:

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

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

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

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

Jed Smith
/dev/random is the good oil, but it can block. Usually you'd be able to get enough for a seed pretty quickly though. /dev/urandom is the non blocking, but not as random
gnibbler
+3  A: 

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

grokus
+1 for pointing out that if you do provide a particular seed, you get a repeatable sequence, which has its uses. We used to use 666 for simulations of universe expansion.
Ewan Todd
@Ewan, *Elite* used a seed for it's universe expansion too ;)
gnibbler
A: 

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

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

Francesco
+1  A: 

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

Mike Graham
+1  A: 

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

gnibbler