[Newbies] Generating a predictable 'atRandom' result for testing

Benjamin Schroeder benschroeder at acm.org
Mon Aug 13 00:35:16 UTC 2007


On Aug 12, 2007, at 8:08 PM, Kyle Hamilton wrote:

> If you can test it for lack-of-randomness, I would presume that  
> you're not trying to test the randomness.
>
> I would create a wrapper to 'get number' -- if the test harness is  
> loaded and a flag is set, have that wrapper return an entry from an  
> array or such, and if not, use atRandom.

Building upon this, it looks like there's a variant of atRandom,  
atRandom:, which takes a random-number generator as a parameter.  
These are instances of Random ...

	generator := Random new.
	100 atRandom: generator. "print me"

You can seed the generators, getting the same sequence every time.

	generatorOne := Random new seed: 1000.
	a := 100 atRandom: generatorOne.

	generatorTwo := Random new seed: 1000.
	b := 100 atRandom: generatorTwo.

	a = b "print me; should be true"

The built-in atRandom uses a generator stored in the Collection  
class, accessible via a class-side method.

	Collection randomForPicking

You could always re-seed that one, but I'm not sure this would  
actually reset the sequence; nor am I sure it would be free of ill  
effects to the rest of the system - so using a generator with  
atRandom: might be a good bet! You could seed the generator  
predictably for testing, and use a standard seed usually.

You can explore all this, if you're interested, by looking at  
implementors of atRandom, and following implementors from there.

Hope this helps,
Benjamin Schroeder



More information about the Beginners mailing list