in reply to Re: When test-driven development just won't do
in thread When test-driven development just won't do
I'm off on a tangent here, so please excuse the off-topicness of this quick example.
How do you test your shuffling or die rolling technique is fair and produces results according to your specifications (say, according to a Gaussing distribution), and isn't biased favouring certain outcomes?
By using Test::LectroTest! Given this trivial implementation of an n-sided die, which returns a number between 1 and the number of sides on your die. Default number of sides is 6.
package Die; sub new { my ( $pck, $sides ) = @_; return bless( { sides => $sides || 6 }, $pck ); } sub roll { my ( $self ) = @_; return int( rand( $self->{ sides } ) ) + 1; } 1;
We then express how we want our die to perform with a Test::LectroTest property that generates a thousand tests with dies with from 1 to 100 sides. We roll the die, and check that the result is legal for this kind of die (eg. within the limits). The second propery generates a thousand six sided dies and rolls each of them once. The results are stored in the the LectroTest controller object with the tcon->label()-call, that automatically spits out the distribution of the roll.
#! /usr/bin/perl use Test::LectroTest; use Die; Property { ##[ x <- Int( range => [ 1, 100 ], sized => 0 ) #]## my $die = Die->new( $x ); my $roll = $die->roll(); ( 0 < $roll && $x >= $roll ); }, name => "My die returns something between 1 and the number of sides + of the die."; Property { ##[ x <- Unit( 6 ) #]## my $die = Die->new( $x ); my $roll = $die->roll(); $tcon->label( $roll ); ( 0 < $roll && $x >= $roll ); }, name => "With a six sided die, I get this distribution";
When I run this, I get:
1..2 ok 1 - 'My die returns something between 1 and the number of sides of +the die.' (1000 attempts) ok 2 - 'With a six sided die, I get this distribution' (1000 attempts) # 18% 2 # 17% 3 # 16% 1 # 16% 5 # 16% 6 # 15% 4
What this means is that the number 2 showed up 18% of the time, 3 17% etc. The numbers don't add up to a 100%, but I presume this is because of rounding in the presentation. In any case, this looks like acceptable behavior for a six sided die to me. I'm sure it's possible to automatically analyze the distribution and build this into the test, but I don't have the time to find out how right now. And please remember that this example was hacked together in a hurry, so I'm sure it can be improved.
I like Test::LectroTest :)
Edit: Removed an erroneous line in the second property.
pernod
--
Mischief. Mayhem. Soap.
Retitled by davido from 'OT: Test::LectroTest and pseudo random distributions'.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Test::LectroTest and pseudo random distributions
by xdg (Monsignor) on Aug 04, 2005 at 16:01 UTC | |
by tmoertel (Chaplain) on Nov 14, 2005 at 22:26 UTC | |
by pernod (Chaplain) on Aug 04, 2005 at 17:17 UTC | |
by xdg (Monsignor) on Aug 04, 2005 at 19:20 UTC | |
by Anonymous Monk on Aug 04, 2005 at 20:22 UTC | |
by chromatic (Archbishop) on Aug 05, 2005 at 00:39 UTC | |
by xdg (Monsignor) on Aug 04, 2005 at 20:32 UTC | |
by Anonymous Monk on Aug 04, 2005 at 20:34 UTC | |
|
Re: Test::LectroTest and pseudo random distributions
by BrowserUk (Patriarch) on Aug 05, 2005 at 06:00 UTC |