我需要得到一个介于 1-200 之间的随机数,但同时我需要防止选择一个已经用于特定 REMOTE_ADDR
的随机数(存储在表格中)。
这是我目前所拥有的(我尝试了几种不同的方法):
$ip = $_SERVER['REMOTE_ADDR'];
$query7 = "
SELECT *
FROM IP
WHERE IP = '$ip'
";
$result7 = mysql_query($query7) or die(mysql_error());
$rows7 = mysql_num_rows($result7);
while ($row7 = mysql_fetch_array($result7)){
$id = $row7['ID'];
}
我正在使用随机数来选择要显示的图像,但我的用户提示为他们选择的图像不够随机;即,同一张图片过于频繁地“随机”选择,有时会一遍又一遍地显示同一张图片。
如果有其他选择,则不必使用 PHP。
最佳答案
类似的东西
// all ids from 1 to 100
$all = array_fill(1, 200, 0);
// remove used
foreach ($used as $i) {
unset($all[$i]);
}
// get survived keys
$keys = array_keys($all);
// get random position, note that the array with keys is 0 based
$j = rand(0, count($all) - 1);
return $keys[$j];
关于php - 获取 1-200 之间的随机数,但忽略从查询中读取的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141278/