我需要在 php 中创建一个随机数生成器,使用 mt_rand
它应该只生成随机数。我知道我需要使用无限循环。我怎样才能做到这一点?
这是我已经尝试过的:
$query = "SELECT * FROM random";
$result = mysqli_query($link,$query);
$rows = mysqli_num_rows($result);
for($j = 0; $j < $rows; ++$j) {
$row=mysqli_fetch_assoc($result);
if($row["randomcol"]==$a) {
do {
echo "n fetched";
echo $row["randomcol"];
echo "n generated";
echo $a;
echo "n repetition";
$a=mt_rand(1,10);
echo "n Just after regeneration";
echo $a;
} while ($row["randomcol"]==$a);
}
}
使用 rand(( 函数。
$random_number = rand(1000,99999); //random number between 1000 and 99999
我在 php 中创建了示例随机生成器函数。您可以根据需要更改长度和字符。
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}