random number generator state
I find I have a need in randomized testing for a shorter version of getstate, even if it _is_ slower to restore. When running exhaustive tests, a failure report should show the start state of the generator. Unfortunately, our current state includes a 625-element array. I want a state that can be read off a report and typed in to reproduce the state. Something a bit like the initial seed, a count of cycle calls, and a few other things. So, in addition to .getstate() and .setstate(...), I'd at least need to have .get_slow_state() and possibly expand what .setstate(...) takes. However, a call to .setstate should reset the counter or all is for naught. That means I need to change the results of .getstate, thus giving me three kinds of input to .setstate: old, new-short, and new-long. In trying to get this to work, I found what might be a bug: code says mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ but probably should be: mt[0] |= 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ In checking into that issue, I went to the original Mersenne-Twister code, and I see the original authors are pursuing a newer generator, dSFMT. I now have a dilemma. Should I continue the work on the original M-T code (which is now seeming problematic for compatibility) or simply make a new generator with similar calls using dSFMT and put the new feature in that where there is no compatibility problem. Which would be more useful for the Python community? --Scott David Daniels Scott.Daniels@Acm.Org
[Scott David Daniels]
I find I have a need in randomized testing for a shorter version of getstate, even if it _is_ slower to restore. When running exhaustive tests, a failure report should show the start state of the generator. Unfortunately, our current state includes a 625-element array. I want a state that can be read off a report and typed in to reproduce the state. Something a bit like the initial seed, a count of cycle calls, and a few other things.
Sounds like you could easily wrap the generator to get this. It would slow you down but would give the information you want. I think it would be a mistake to complexify the API to accomodate short states -- I'm not even sure than they are generally useful (recording my initial seed and how many cycles I've run through is only helpful for sequences short enough that I'm willing to rerun them). I'm curious what your use case is. Why not just record the the sequence as generated -- I don't see any analytic value to just knowing the initial seed and cycle count. Ability to print out a short state implies that you are using only a small subset of possible states (i.e. the ones you can get to with a short seed). A short state print out isn't even possible if you actually have a random initial state (every state having an equal chance of being the starting point).
In trying to get this to work, I found what might be a bug: code says mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ but probably should be: mt[0] |= 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
Please file a bug report for this and assign to me. I put in the existing MT code and took it directly from the author's published (and widely tested code). Also, our tests for MT exactly reproduce their published test sequence. But, if there is an error, I would be happy to fix it.
In checking into that issue, I went to the original Mersenne-Twister code, and I see the original authors are pursuing a newer generator, dSFMT.
The MT itself has the advantage of having been widely exercised and tested. The newer generator may have more states but has not been as extensively tested.
I now have a dilemma. Should I continue the work on the original M-T code (which is now seeming problematic for compatibility) or simply make a new generator with similar calls using dSFMT and put the new feature in that where there is no compatibility problem. Which would be more useful for the Python community?
It's not hard to subclass Random and add different generators. Why not publish some code on ASPN and see how it gets received. I've put a recipe there for a long period generator, http://code.activestate.com/recipes/576707/ , but there doesn't seem to have been any real interest in generators with longer periods than MT. Raymond
On Sat, Aug 15, 2009 at 8:54 PM, Scott David
Daniels
[...] input to .setstate: old, new-short, and new-long. In trying to get this to work, I found what might be a bug: code says mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ but probably should be: mt[0] |= 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
I'm 92.3% sure that this isn't a bug. For one thing, that line comes directly from the authors' code[1], so if it's a bug then it's a bug in the original code, dating from 2002; this seems unlikely, given how widely used and (presumably) well-scrutinized MT is. For a more technical justification, the Mersenne Twister is based on a linear transformation of a 19937-dimensional vector space over F2, so its state naturally consists of 19937 bits of information, which is 623 words plus one additional bit. In this implementation, that extra bit is the top bit of the first word; the other 31 bits of that first word shouldn't really be regarded as part of the state proper. If you examine the genrand_int32 function in _randommodule.c, you'll see that the low 31 bits of mt[0] play no role in updating the state; i.e., their value doesn't affect the new state. So using mt[0] |= 0x80000000UL instead of mt[0] = 0x80000000UL during initialization should make no difference to the resulting stream of random numbers (with the possible exception of the first random number generated). [1] http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c Mark
Scott David Daniels wrote:
I find I have a need in randomized testing for a shorter version of getstate, even if it _is_ slower to restore. When running exhaustive tests, a failure report should show the start state of the generator. Unfortunately, our current state includes a 625-element array.
Do you need to use the Mersenne Twister in particular for this? There are other kinds of generator with very long cycles and good statistical properties, that can easily be restored to any state in constant time given an initial state and a count. Let me know if you're interested and I can give you further details. -- Greg
Raymond Hettinger wrote:
[Scott David Daniels]
I find I have a need in randomized testing for a shorter version of getstate, even if it _is_ slower to restore. [blah about big state]
Sounds like you could easily wrap the generator to get this. It would slow you down but would give the information you want. Well, I was thinking that this might be generally useful for randomized testing.
I think it would be a mistake to complexify the API to accomodate short states -- I'm not even sure than they are generally useful (recording my initial seed and how many cycles I've run through is only helpful for sequences short enough that I'm willing to rerun them). Right, that was what I was asking about. The complexity of the change grew on me; I hadn't realized at the outset it would be more than adding a counter internally. Consider me officially dissuaded.
I'm curious what your use case is. Why not just record the the sequence as generated -- I don't see any analytic value to just knowing the initial seed and cycle count. I'm building data structures controlled by an rng, and then performing sequences of (again randomly controlled) operations on those data structures, check all invariants at each step. I then lather, rinse, repeat recording the start of each failing experiment. In the morning I come in and look for commonality in the cases I see. Having the short state means I means I can easily rebuild the data structure and command list to see what is going on. I prune commands, simplify the tree, and thus isolate the problem I found.
Ability to print out a short state implies that you are using only a small subset of possible states (i.e. the ones you can get to with a short seed). Well, as you see above, I do capture the seed. I realize that the time- constructed seeds are distinct from identically provided values as small ints, and I also mark when the rng gets called by set_state to indicate
I did enough experimenting to see that if I simply provide access to run N cycles of the block, I can actually do 2**32 cycles in feasible time, so I have a pair of counters, and the code should take long enough for eternity to show up before the wrap. My short state is: seed, block_index, cycles_low, cycles_high, floating (block_index + 625 * (cycles_low + (cycles_high << 32)) is the position, and could be done as such; the pieces reflect the least-expensive cost in performance to the rng. floating is simply the same final floating piece that the state keeps now. that I then know nothing about the seed.
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ but probably should be: mt[0] |= 0x80000000UL; /* MSB is 1; assuring non-zero initial array*/ Please file a bug report for this and assign to me.... Also, our tests for MT exactly reproduce their published test sequence.
I've been assured it is not a bug, and I filed no report since I had just arrived at the point of suspicion. To summarize, I am officially dissuaded, and will post a recipe if I get something nice working. --Scott David Daniels Scott.Daniels@Acm.Org
participants (4)
-
Greg Ewing
-
Mark Dickinson
-
Raymond Hettinger
-
Scott David Daniels