我想做一个随机数字(8位数)... 像..
反复直到我得到1000个不同的数字
这是我的代码:
<?php
$digits_needed=8;
$random_number=''; // set up a blank string
$count=0;
while ( $count < $digits_needed ) {
$random_digit = mt_rand(0, 9);
$random_number .= $random_digit;
$count++;
}
echo "The random $digits_needed digit number is 0896$random_number<br>";
?>
答案 0 :(得分:2)
您可以获得1000个这样的8位数字
$count = 1000;
for ($i = 0; $i < $count; $i ++) {
$random_digit = mt_rand(0, 99999999);
echo "The random 8 digit number is 0896 $random_digit<br>";
}
答案 1 :(得分:0)
试试这个
function randStrGenDigit($len){
$result = "";
$chars = "0123456789";
$charArray = str_split($chars);
for($i = 0; $i < $len; $i++){
$randItem = array_rand($charArray);
$result .= "".$charArray[$randItem];
}
return $result;
}
$digits_needed=8;
$random_number=''; // set up a blank string
$count=0;
for($i=0;$i<1000;$i++) {
echo "The random ".$digits_needed." digit number is 0896 ".$randStrGenDigit($digits_needed)."<br>";
}