Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is suggested seed value to use with random.seed()?

Tags:

python

random

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.

like image 615
AJ. Avatar asked Nov 09 '09 18:11

AJ.


People also ask

What is the seed value in the random module?

The random module uses the seed value as a base to generate a random number. Generally, the seed value is the previous number generated by the generator. However, When the first time you use the random generator, there is no previous value. So by-default current system time is used as a seed value.

Why does the same seed produce the same random number?

For a specific seed value, the random state of the seed function is saved. So, the particular seed value will produce the same random numbers even on multiple executions. The same seed value led to the same random number generation even on different machines given the environment remains the same.

What is the use of random seed in Java?

Also, random.seed () is useful to reproduce the data given by a pseudo-random number generator. By re-using a seed value, we can regenerate the same data multiple times as multiple threads are not running. When we supply a specific seed to the random generator, every time you execute a program, you will get the same numbers.

How to generate random values from a random seed in Python?

Let's say 'random.seed' gives a value to random value generator ('random.randint ()') which generates these values on the basis of this seed. One of the must properties of random numbers is that they should be reproducible. When you put same seed, you get the same pattern of random numbers. This way you are generating them right from the start.


2 Answers

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.

like image 80
Jed Smith Avatar answered Sep 19 '22 14:09

Jed Smith


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.

like image 25
grokus Avatar answered Sep 18 '22 14:09

grokus