ルーレット選択
#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @rate = (60,30,10);
my @result1;
foreach ( 1 .. 100000 ) {
my $idx = roulette1(@rate);
$result1[$idx]++;
}
print Dumper \@result1;
my @result2;
foreach ( 1 .. 100000 ) {
my $idx = roulette2(@rate);
$result2[$idx]++;
}
print Dumper \@result2;
sub roulette1 {
my @rate = @_;
my @total;
$total[0] = $rate[0];
for ( my $i = 1 ; $i < @rate ; $i++ ) {
$total[$i] = $total[ $i - 1 ] + $rate[$i];
}
my $rand = int( rand( $total[ @total - 1 ] ) ) + 1;
my $idx = 0;
while ( $rand > $total[$idx] ) {
$idx++;
}
return $idx;
}
sub roulette2 {
my @rate = @_;
my $total;
foreach my $n (@rate) {
$total += $n;
}
my $rand = int( rand($total) ) + 1;
my $idx = 0;
for ( ; $idx < @rate ; $idx++ ) {
$rand -= $rate[$idx];
if ( $rand <= 0 ) {
last;
}
}
return $idx;
}