1stPHP
乱数、ランダムな文字列
ソース
<?php
include "../../d/common.php";
$common = new common;
$common->showHead("乱数、ランダムな文字列-".$common->cfg[title],"../../css/main.css","","../../d/count/top.cgi");
$common->showAdd();
$common->smpmenu("乱数、ランダムな文字列","./rand.php");
$length = $_POST["length"];
//乱数の最大値から最大桁数をセットします。
$randmax = (string)mt_getrandmax();
$maxlen = strlen($randmax)-1;
//桁数を1〜$maxlenの間にします
if (!$length || ($length < 1 || $length > $maxlen)){
$length = 4;
}
//フォームの表示
print '
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<p>
乱数を
<input type="text" name="length" size="4" value="'.$length.'" />
桁で
<input type="submit" name="submit" value="作成" />
</p>
</form>
';
if ($_POST["submit"]) {
//乱数生成器にシードを与えます
mt_srand((double) microtime() * 10000000);
//累乗を返します
$min = pow(10,$length-1);
$max = pow(10,$length)-1;
//$minから$maxの間で乱数を生成します。
$rand = mt_rand($min,$max);
print "<h3>$length.桁の乱数</h3>";
print "<p>$rand</p>";
//乱数を文字列へキャストし、md5ハッシュを作成します
$randstr = md5((string)mt_rand());
print "<h3>ランダムな文字列</h3>";
print "<p>$randstr</p>";
}
$common->showFoot();
?>