4
$\begingroup$

Well, I don't really have much to add to the title. I tend not to use seeds in preudo-random number generation, but they are handy when an initial research project that includes simulations expands to include more aspects of the same simulation study.

In that case, if a seed was used, one can perform only the additional tasks.

So are there any "good practice" tips for choosing seeds, or they can be chosen... at random?

$\endgroup$
  • 1
    $\begingroup$ This is somewhat vague and broad. An example of a more specific related question is stats.stackexchange.com/questions/80407. $\endgroup$ – whuber Nov 18 '19 at 23:25
  • 3
    $\begingroup$ It very much depends on the generator. For some generators, such as PCG, any seed will do; for others, though, such as Mersenne Twister, bad things can happen; nearby seed values give nearby streams for a considerable time, etc. For more, see pcg-random.org/other-rngs.html, where the defects of various generators are discussed in some detail. $\endgroup$ – jbowman Nov 18 '19 at 23:30
  • 1
    $\begingroup$ You might want to look at Matsumoto et al.'s "Common Defects in Initialization of Pseudorandom Number Generators". $\endgroup$ – Mars Nov 25 '19 at 5:23
  • 1
    $\begingroup$ Not everyone is as negative about Mersenne Twisters as O'Neill (pcg-random). Another issue is that seeds with many zero bits take a long time to get out of that pattern (Pannetton et al. "Improved Long-Period Generators Based on Linear Recurrences Modulo 2"). But any decent implementation of Mersenne Twister will initialize its state with another, simpler PRNG. The Twister should then be iterated at least 624 times. If you do that, you shouldn't have a problem with nearby seeds, and the zeros issue is unlikely to be a serious problem. $\endgroup$ – Mars Nov 25 '19 at 5:25
  • 1
    $\begingroup$ Whether you can use the same seed all of the time, as some people do, depends on the application. (For my uses, that would be a bad idea.) It shouldn't matter what seed you use, in the sense that weird results should be rare, just as low-probability outcomes are rare. $\endgroup$ – Mars Nov 25 '19 at 5:29
1
$\begingroup$

You might want to look at Matsumoto et al.'s "Common Defects in Initialization of Pseudorandom Number Generators".

In one sense shouldn't matter what seed you use, in that with a good PRNG, weird results should be rare, just as low-probability outcomes in nature should be rare. Whether you can use the same seed all of the time, as some people do, depends on the application. I personally would never do this, but I run Monte Carl simulations in which different seeds might produce different patterns of outcomes, and I want to know whether this happens. The data that I ultimately care about is is the collection of data I get when I run the simulation many times with the same parameters but different seeds. I can then perform statistics on this data, plot summary data, etc.

Mersenne Twister and other algorithms:

Concerning jbowman's comment, not everyone is as negative about Mersenne Twisters as O'Neill (pcg-random.org/other-rngs.html). If you don't care about M.T.'s extremely long period (arguably overkill), there are better algorithms (including, I think, O'Neill's PCG algorithms, though there's been some debate between O'Neill and Vigna about whose algorithms are better). However, M.T. is still common in software packages that I trust, where PRNG quality matters. (By contrast, some Java rand() functions are not as good.)

Any decent implementation of Mersenne Twister will initialize its state with another, simpler PRNG, which might differ for different implementations (cf. these remarks by Matsumoto). The Twister should then be iterated at least 624 times (better yet, twice that number) before using its output. If you do that, you shouldn't have a problem with nearby seeds. (A high-quality agent-based modeling library, MASON uses Mersenne Twister with this scheme, and if you tell it to perform multiple runs with the same parameters, by default it will simply increment the first seed for each subsequent run.)

Another issue with Mersenne Twisters is that if its internal 624x32-bit state has many zero bits, it takes many iterations to get out of that pattern. (See Pannetton et al. "Improved Long-Period Generators Based on Linear Recurrences Modulo 2", which describes a better algorithm, WELL, although with shorter periods.) However, if you initialize the Mersenne Twister in the usual way with another pseudorandom number generating algorithm, I would think that the zeros issue would be unlikely to be a serious problem, since it should be rare that such an algorithm gives a Mersenne Twister a starting state with a lot of zero bits. (The most recent version of M.T. has less of a problem with zero bits; see Saito and Matsumoto, "SIMD-oriented Fast Mersenne Twister: a 128-bit Pseudorandom Number Generator", pages 13-14.)

(N.B. Extra details on Mersenne Twisters: The remarks above are for the most common kind of Mersenne Twister with a 624x32-bit internal state and a period of $2^{19937}−1$. If you seed it directly, you need to provide 624 32-bit numbers as a seed. Since it's usually undesirable to have to do that, by default you give a wrapper function a 32-bit (or possibly 64-bit) seed, which passes it to a simpler, lower-quality pseudorandom number generator. This is used to generate the 624x32-bit seed for the Mersenne Twister. However, the way that a M.T. works is that it takes successive numbers from its state, passes them to a function that rearranges the bits in the number, and outputs the result. When all 624 numbers are used this way, it performs an operation on the entire internal state (including a step known as a "twist") to generate a new 624x32-bit state. This is why you should not use the first 624 or 1248 outputs; they are partially the result of a lower-quality pseudorandom number generator, and are not due to the full Mersenne Twister algorithm. Kneusel's introductory book on PRNGs includes an introduction to Mersenne Twisters, but read the xorshift section first.)

(Other introductory texts--not quite as easy as Kneusel, and they don't necessarily cover Mersenne Twisters, if that's what you're interested in--include:

$\endgroup$
  • $\begingroup$ Thanks, and thanks for the pointer to the Introductory book. Just to check that I understand half of what you write, when we say "throw out the first 624 outputs", we mean the first 624 numbers that will come out of the MT after a "generate a pseudo-random sequence" command? $\endgroup$ – Alecos Papadopoulos Nov 27 '19 at 9:26
  • $\begingroup$ Yes, that's correct. I'll clarify the text, and also add that just to be sure, I prefer to throw out twice that many numbers--just takes a moment, and then you can be sure that M.T. has (probably) performed sufficient magic. Maybe I should also add a couple more literature references, but Kneusel's the easiest starting point I've found. $\endgroup$ – Mars Nov 27 '19 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.