On Mon, Jan 18, 2010 at 2:24 AM, Drew Farris <drew.far...@gmail.com> wrote: > On Sun, Jan 17, 2010 at 9:10 PM, Sean Owen <sro...@gmail.com> wrote: >> There are already cases where code needs to control the seed (mostly >> to serialize/deserialize the exact state of an object). I don't think >> that's the issue per se? The issue is when an RNG lives beyond one >> test, and there are legitimate reasons that may be so. > > Ahh, ok, I wasn't really considering this. Would it be sufficient to > assign the RNG to a static field in the test class in this case? If it > needed to live across multiple classes, it could be public. > Nevertheless..
Well that would create the problem rather than solve it but since the problem already does (or will) exist legitimately in the main code, you could say sure, why not? After all in some class that's instantiate a lot, doesn't make sense from an efficiency standpoint to initialize an RNG per instance, and so it's static, and there you go, problem. (This wouldn't be a good change for tests though since a statically-initialized RNG would be created before setUp() set the framework to test mode. But then again, that's another example of the actual issue at hand.) The real fix is centralizing management of Random, tracking them, and being able to reset them all "remotely". I know how that could be done. > I suspect I'm missing something here because I don't understand how > randomness is used in the non-test code or specifically how the RNG's > are managed. I was (falsely, likely) assuming that the non-test code > didn't obtain the RNG itself but rather had it provided/injected by an > external source. In the context of a test, something from > getTestRandom() which uses a fixed seed could be injected, while in > production code something else would be. > Randomness is used outside of tests to, for example, sample 10% of a data set for example. Or k-means. It is injected already -- that's the purpose of having "getRandom()" and not "getTestRandom()". That's the means by which a different fixed-seed RNG can be provided when run in a test harness. You couldn't do that with two methods: they'd each return a normal or fixed RNG, and the code could only call one.