Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • It would have been easier to follow what’s going on if you have edited the code prior to writing, so as to highlight what you are talking about. I might have written the first program like this:

    use Data::Dumper;
    use Math::Random::MT qw(rand);

    sub min_idx {
        my $idx = 0;
        for my $i ( 1 .. $#_ ) {
            $idx = $_[$idx] > $_[$i] ? $i : $idx;
        }
        return $idx;
    }

    sub run {
        my ( $arg ) = @_;
        my @n;
        for my $i (1 .. $arg->{count} ) {
            my @d = $arg->{distrib}->( @{ $arg->{weights} } );
            $n[ min_idx @d ]++;
        }
        return @n;
    }

    print Dumper [ run {
        count   => 1E5,
        weights => [ 1, 2 ],
        distrib => sub { map { $_ * rand } @_ },
    ) ];

    Then instead of repeating the program for the second formula (with slight variations in variable names that obscure your point) you could simply have written that this time, we will be using this:

        count   => 1E5,
        weights => [ 1, 2, 3 ],
        distrib => sub { map { -log( 1 - rand ) / $_ } @_ },

    Much easier to see what the difference is.