サイコロの度数分布アゲイン
サイコロの度数分布 - 永字八法の焼き直し。
今見直したら、もうちょっとすっきりできそうだったので。
use strict; my $aspect = 6; # サイコロの面数 my $num = 3; # サイコロの個数 my @number = ( 1 ); my @temp = (); my $mother = 1; foreach ( 1..$num ) { @temp = (); my $last = $#number; $mother *= $aspect; foreach my $i ( 0..$last ) { $number[$i] or next; foreach my $asp ( 1..$aspect ) { $temp[$i+$asp] += $number[$i]; } } @number = @temp; } my $max = 0; map { $max < $_ and $max = $_ } @number; my $count = -1; foreach my $number ( @number ) { ++ $count; $number or next; printf "%3d : %10d / ", $count, $number; print "$mother "; print '■' x int(20*$number/$max); print "\n"; }
実行例
3 : 1 / 216 4 : 3 / 216 ■■ 5 : 6 / 216 ■■■■ 6 : 10 / 216 ■■■■■■■ 7 : 15 / 216 ■■■■■■■■■■■ 8 : 21 / 216 ■■■■■■■■■■■■■■■ 9 : 25 / 216 ■■■■■■■■■■■■■■■■■■ 10 : 27 / 216 ■■■■■■■■■■■■■■■■■■■■ 11 : 27 / 216 ■■■■■■■■■■■■■■■■■■■■ 12 : 25 / 216 ■■■■■■■■■■■■■■■■■■ 13 : 21 / 216 ■■■■■■■■■■■■■■■ 14 : 15 / 216 ■■■■■■■■■■■ 15 : 10 / 216 ■■■■■■■ 16 : 6 / 216 ■■■■ 17 : 3 / 216 ■■ 18 : 1 / 216