in reply to is rand random enough to simulate dice rolls?

Hello monks and nuns,

Following your advises and adding what I received on #perl irc channel, I both made the eventually failing test not critical (using SKIP ) and I also let the user to provide their custom rand function. Relevant part are:

# ./Games/Dice/Roller.pm sub new{ my $class = shift; my %opts = @_; if ( defined $opts{sub_rand} ){ croak "sub_rand must be a code reference meant to replace core + rand function" unless ref $opts{sub_rand} eq 'CODE'; } return bless { sub_rand => $opts{sub_rand} // sub{ rand($_[0]) }, }, $class; } sub single_die{ my $self = shift; my $sides = shift; croak "single_die expect one argument" unless $sides; croak "Invalid side [$sides]" unless $sides =~/^(\d+)$/; $sides = $1; return 1 + int( $self->{sub_rand}($sides) ); } # ./t/01-single-die.t my $tenk; $tenk += $dice->single_die(6) for 1..10000; my $avg = $tenk/10000; # $avg = 5; # uncomment this line to provoke the warning if ( ($avg < 3.4) or ($avg > 3.6) ){ diag( "\n\n\nPROBLEM: you got an average of $avg while was expec +ted a value > 3.4 and < 3.6\n\n\n". "The average was made on 10000d6 rolls.\n". "This can happen in old Perl distribution on some platform +.\n". "You can use sub_rand => sub{.. during constructor to prov +ide an\n". "alternative to core rand function (using rand from Math:: +Random::MT for example).\n\n\n\n" ); } else { pass "average randomness ok (3.4 < 10000d6 / 10000 < 3.6)" ; } # ./t/07-rand-custom-funcion.t SKIP: { eval { require Math::Random::MT }; skip "Math::Random::MT not installed", 2 if $@; my $gen = Math::Random::MT->new(); my $mt_dicer = Games::Dice::Roller->new( sub_rand => sub{ my $sides = shift; return $gen->rand( $sides ); }, ); my ($res, $descr) = $mt_dicer->roll('13d4kh7'); ok( $res >= 7, "succesfully used rand from Math::Random::MT as ran +dom number generator"); ok( $res <= 28, "succesfully used rand from Math::Random::MT as ra +ndom number generator") }

The house of this toy project (for hippo' happines ;) is at gitlab

Thanks to you all

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.