views:

289

answers:

6

Consider code like this (Python):

import random

for i in [1, 2, 3, 4]:
    random.seed(i)
    randNumbers = [random.rand() for i in range(100)] # initialize a list with 100 random numbers
    doStuff(randNumbers)

I want to make sure that randNumbers differ significantly from one call to another. Do I need to make sure the seed numbers differ significantly between the subsequent calls, or is it sufficient that the seeds are different (no matter how)?

To the pedants: please realize the above code is super-over-simplified

+1  A: 

Generally speaking, you only seed your random number generator when you need the random numbers to be generated in identical fashion each time through. This is useful when you have a random component to your processing, but need to test it and therefore want it to be consistent between tests. Otherwise, you let the system seed the generator itself.

In otherwords, by seeding the random number generator with specific pre-defined seeds, you are actually reducing the randomness of the system as a whole. The random numbers generated when using a seed of 1 are indeed psuedo-randomly different from that with a seed of 2, but a hard coded seed will result in repeated random sequences in each run of the program.

Matt
+3  A: 

Short answer: Avoid the re-seeding, as it doesn't buy you anything here. Long answer below.


That all depends on what exactly you need. In Common defects in initialization of pseudorandom number generators it is outlined that linear dependent seeds (which 1, 2, 3, 4 definitely are) are a bad choice for initializing multiple PRNGs, at least when used for simulation and desiring uncorrelated results.

If all you do is rolling a few dice, or generating some pseudo-random input for something uncritical, then it very likely doesn't matter.

Note also that using some classes of a PRNG itself for generating seeds have the same problem in generating linear dependent numbers (LCGs spring to mind).

Joey
A: 

The seeds themselves should be random so that the output is unpredictable. There can be problems if the seeds differ only in one or two bits (as this question demonstrates).

Dan Dyer
Manually seeding usually implies that the output doesn't need to be unpredictable. And how much the seeds should differ greatly depends on the algorithm of the PRNG.
Joey
It implies the output doesn't need to be unpredictable, but that is also assuming the question asker actually realizes that seeded generators result in the same sequence of numbers.
Matt
@Johannes: Absolutely, but if you do truly want unpredictable output then you don't want someone to be able to guess the seed. So for this reason it's best that the seed itself is completely unpredictable (e.g. from /dev/random).
Dan Dyer
A: 

It depends upon the application for which you're using the PRNG. If you're using something that needs to be cryptographically sound, then the seeds generally need to be extremely difficult to deduce based on the output, different every time the application runs, difficult to simply guess, and impossible to determine by reverse engineering the application (i.e. they can't be hard coded).

If your goal is a game, your requirements may be different. For example, if you're controlling computer strategy, but the computer's strategy remains the same for all runs of the game, you may have an easily beatable game. Then again, you may want that for "easy" mode.

atk
If this has anything to do with crypto, then MT19937 is a very wrong generator to begin with, though.
Joey
A: 

Hi

You seem to want pseudo-random numbers that aren't pseudo-random, with a higher probability of consecutive numbers being 'significantly' different than pseudo-randomness requires. I doubt that any common prng will do this, whatever your seeding strategy.

Regards

Mark

High Performance Mark
A: 

If your random number generator is high quality, it shouldn't matter how you seed it. In fact, the best practice would be to seed it only once. Random number generators are designed to have certain statistical behavior once they're started. Frequently reseeding effectively creates a different random number generator, one that may not be as good.

Randomly selecting seeds sounds like a good idea, but it isn't. In fact, because of the "birthday paradox," there's a surprisingly high probability that you'll pick the same seed twice.

John D. Cook