
■=Perl, ■=結果
「ココの俳句?教室」で rand関数から乱数を発生させ、自動的に適当な行のデータを表示させました。しかし本当にちゃんと全ての行から選んでいるのでしょうか。少し不安になり検証してみることにしました。先ほどの「ココの俳句?教室」では3種類のファイル毎に乱数の最大値が異なりました(各ファイルの行数が異なる)が、ここでは便宜上、ゼロから9までの数を3種類発生させることにします。そしてそれを一万回繰り返し、数字毎に何回発生したかを調べてみます。 |
rand(発生させる乱数の最大値) |
#!/usr/local/bin/perl
$i = 0;
# 乱数作成
while ($i < 10000) {
$rand1 = int(rand(10));
$rand2 = int(rand(10));
$rand3 = int(rand(10));
if ($rand1 == 0) { $zero++ }
if ($rand1 == 1) { $one++ }
if ($rand1 == 2) { $two++ }
if ($rand1 == 3) { $three++ }
if ($rand1 == 4) { $for++ }
if ($rand1 == 5) { $five++ }
if ($rand1 == 6) { $six++ }
if ($rand1 == 7) { $seven++ }
if ($rand1 == 8) { $eight++ }
if ($rand1 == 9) { $nine++ }
if ($rand2 == 0) { $tow_zero++ }
if ($rand2 == 1) { $tow_one++ }
if ($rand2 == 2) { $tow_two++ }
if ($rand2 == 3) { $tow_three++ }
if ($rand2 == 4) { $tow_for++ }
if ($rand2 == 5) { $tow_five++ }
if ($rand2 == 6) { $tow_six++ }
if ($rand2 == 7) { $tow_seven++ }
if ($rand2 == 8) { $tow_eight++ }
if ($rand2 == 9) { $tow_nine++ }
if ($rand3 == 0) { $three_zero++ }
if ($rand3 == 1) { $three_one++ }
if ($rand3 == 2) { $three_two++ }
if ($rand3 == 3) { $three_three++ }
if ($rand3 == 4) { $three_for++ }
if ($rand3 == 5) { $three_five++ }
if ($rand3 == 6) { $three_six++ }
if ($rand3 == 7) { $three_seven++ }
if ($rand3 == 8) { $three_eight++ }
if ($rand3 == 9) { $three_nine++ }
$i++;
}
print "Content-type: text/html\n\n";
print "[\$rand1]0=$zero, 1=$one, 2=$two, 3=$three, 4=$for, 5=$five, 6=$six, 7=$seven, 8=$eight, 9=$nine<br>\n";
print "[\$rand2]0=$tow_zero, 1=$tow_one, 2=$tow_two, 3=$tow_three, 4=$tow_for, 5=$tow_five, 6=$tow_six, 7=$tow_seven, 8=$tow_eight, 9=$tow_nine<br>\n";
print "[\$rand3]0=$three_zero, 1=$three_one, 2=$three_two, 3=$three_three, 4=$three_for, 5=$three_five, 6=$three_six, 7=$three_seven, 8=$three_eight, 9=$three_nine<br>\n";
exit;
|
$rand1 = int(rand(10)); |
while ($i < 10000) { }
|
if ($rand1 == 0) { $zero++ }
|
それでは上のスクリプトを実行してみましょう。1万回を3種類ですが、さすがに早いですね。
上と比べると、ずいぶん短くなりました。ついでに結果を数値だけでレポートするのでは味気ないのでグラフにしてみました。
#!/usr/local/bin/perl
$i = 0;
$h = 0;
print "Content-type: text/html\n\n";
# 乱数作成
$times = 10000; #乱数発生回数
while ($i < $times) {
$rand1[int(rand(10))]++;
$rand2[int(rand(10))]++;
$rand3[int(rand(10))]++;
$i++;
}
$by = 0.5; #グラフの長さ調整
print "<center>\n";
print "<h2>$times回の乱数発生結果</h2>\n";
print qq|<table border="1"><tr><td>乱数</td><td align="center">発生回数
乱数1<img src="../images/reddot.gif" width="20" height=10">
乱数2<img src="../images/greendot.gif" width="20" height=10">
乱数3<img src="../images/bluedot.gif" width="20" height=10"></td></tr>\n|;
while ($h < 10) {
print qq|<tr><td align="right">$h</td>
<td><img src="../images/reddot.gif" width="|;
print $rand1[$h] * $by;
print qq|" height="10"><font size="-2">$rand1[$h]</font><br>
<img src="../images/greendot.gif" width="|;
print $rand2[$h] * $by;
print qq|" height="10"><font size="-2">$rand2[$h]</font><br>
<img src="../images/bluedot.gif" width="|;
print $rand3[$h] * $by;
print qq|" height="10"><font size="-2">$rand3[$h]</font></td></tr>\n|;
$h++;
}
print "</table>";
exit;
|
$rand1[int(rand(10))]++; |
print qq|<tr><td align="right">$h</td> <td><img src="../images/reddot.gif" width="|; print $rand1[$h] * $by; print qq|" height="10"><font size="-2">$rand1[$h]</font><br> |
結果的にはゼロから9まで、それ程の差もなく発生できていました。
| | Back | Coco's Home | ココとPerlで遊びませんか | |