Skip to content

Instantly share code, notes, and snippets.

@pjxiao
Created June 28, 2012 02:52
Show Gist options
  • Save pjxiao/3008520 to your computer and use it in GitHub Desktop.
Save pjxiao/3008520 to your computer and use it in GitHub Desktop.
display distribution mt_rand return value. / mt_rand 関数が返した値の分布を表示する。
<?php
// 統計学的になんの意味があるのかは知りません。
// 実行中にリターンキーをおすとぶっ壊れます。
// 乱数の上限値
$N = 50;
// printf で表示する数値の幅
$w = 3;
// 改行調整用変数
$m = 10;
// 試行回数
$r = 2000;
// 画面準備
for ($i = 0; $i < $r / $m; $i++) {
echo str_repeat(' ', $N * $w);
echo "\n";
}
echo "\r";
for ($i = 0; $i < $N; $i++) {
printf("%{$w}d ", $i + 1);
}
echo "\033[1A";
echo "\r";
// 試行開始
for ($i = 0; $i < $r; $i++) {
$rand_n = mt_rand(1, $N);
$rand_c[$rand_n]++;
printf("\033[%dA", $rand_c[$rand_n]);
printf("\033[%dC", ($rand_n * ($w + 1)) - $w);
printf('%2s', '+');
echo "\r";
printf("\033[%dB", $rand_c[$rand_n]);
echo "\r";
}
// 終了処理
echo "\033[10B";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment