Generating a predictable 'atRandom' result for testing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating a predictable 'atRandom' result for testing

John Almberg
I'm trying to write a test for a method that uses atRandom to choose  
a random integer. Is there a way to seed the random number generator  
to generate a predictable sequence of numbers, so I end up with  
testable results?

TIA: John

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Generating a predictable 'atRandom' result for testing

Kyle Hamilton
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.

-Kyle H

On Aug 12, 2007, at 11:51 AM, John Almberg wrote:

> I'm trying to write a test for a method that uses atRandom to  
> choose a random integer. Is there a way to seed the random number  
> generator to generate a predictable sequence of numbers, so I end  
> up with testable results?
>
> TIA: John
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Generating a predictable 'atRandom' result for testing

Benjamin Schroeder-2

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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Generating a predictable 'atRandom' result for testing

John Almberg
In reply to this post by Kyle Hamilton
Actually, eventually I *would* like to test the random distribution  
of the results, but as a first go, I'm trying to test the rest of the  
method, that varies with the randomness.

-- John

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.
>
> -Kyle H
>
> On Aug 12, 2007, at 11:51 AM, John Almberg wrote:
>
>> I'm trying to write a test for a method that uses atRandom to  
>> choose a random integer. Is there a way to seed the random number  
>> generator to generate a predictable sequence of numbers, so I end  
>> up with testable results?
>>
>> TIA: John
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Marketing for On-line Collectible Dealers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Identry, LLC
John Almberg
(631) 546-5079
[hidden email]
www.identry.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Generating a predictable 'atRandom' result for testing

John Almberg
In reply to this post by Benjamin Schroeder-2
This sounds promising. I'll give it a whirl tomorrow. Thanks!

-- John

> generatorOne := Random new seed: 1000.
> a := 100 atRandom: generatorOne.
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Generating a predictable 'atRandom' result for testing

Hilaire Fernandes-4
John Almberg a écrit :
> This sounds promising. I'll give it a whirl tomorrow. Thanks!
>
> -- John
>
>>     generatorOne := Random new seed: 1000.
>>     a := 100 atRandom: generatorOne.
>>


This is indeed the way to go. I have being using it to generate
repeatable random sequences.

Hilaire

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners