[Tutor] How to test function using random.randint()?

Alan Gauld alan.gauld at btinternet.com
Sat Mar 19 05:04:32 EDT 2016


On 19/03/16 04:26, boB Stepp wrote:
> If I had a function to roll a die, such as:
> 
> How would I write unit tests for this?  

Ben has given the general case answer.

> And I do not see how I can test for an appropriate "randomness" to the
> numbers the function generates without to a lot of iterations,

And you have answered the specific case where it is the randomness
itself that you are testing. In that case you need to generate
a lot of iterations (several thousand say) and store the results
then analyze the spread against the desired distribution pattern.

There is no shortcut. And if the nature of the randomness is
important, as it often is in simulations etc you have to do it.

But in most cases the amount of randomness is not critical
and you are using a well known and tested RNG so you only
need to check the scaling is producing the right output range.
That does mean you need to check the boundary conditions
carefully and that may also involve a loop to create enough
samples to be sure. One of the most common errors with RNGs
is to generate 1 less than the desired range.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list