Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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'.


In reply to Test::LectroTest and pseudo random distributions by pernod
in thread When test-driven development just won't do by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (15)
    As of 2014-07-30 08:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls