On Mon, Jan 18, 2010 at 3:58 AM, Sean Owen <sro...@gmail.com> wrote: > The real fix is centralizing management of Random, tracking them, and > being able to reset them all "remotely".
In what cases would you want to reset them all remotely, at the beginning of each test? > 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. I was suggesting the RNG could be specified outside of the code instead of inside of the code. For example, instead of: GenericRecommenderIRStatsEvaluator() { random = RandomUtils.getRandom(); } You could have: GenericRecommenderIRStatsEvaluator(Random r) { random = r; } In tests you call Random r = RandomUtil.getTestRandom() ev = new GenericRecommenderIRStatsEvaluator(r); In production code you call: Random r = RandomUtil.getRandom(); ev = new GenericRecommenderIRStatsEvaluator(r); The alternative would be to leave the constructor for GenericRecommenderIRStatsEvaluator at it is and provde a way to set the value of the field 'random' to something for testing, e.g: the return value of RandomUtil.getTestRandom() For example in tests: Random r = RantomUtil.getTestRandom() ev = new GenericRecommenderIRStatsEvaluator(); ev.setRandomSeed(r); While production code would remain unchanged. This won't necessarily work in all cases depending upon what sort of things happen in the constructor. As Ted pointed out, doing the above would require a fair amount of thought and work, there's no single approach to introducing this type of injection that will work everywhere. Drew