我想创建一个Bingo脚本。我已经用随机数编码了一个6x6模式,每行都有一定的范围。但现在它多次输出相同的数字,我希望它是所有随机数,有人可以帮助我吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Indzendopdracht 051R3</title>
</head>
<body>
<?PHP
function printBingocard(){
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
$rand_keys = array_rand($bingoNumbers, 1);
$rows = array();
foreach($bingoNumbers["rij_1"] as $bn_rij1){
if($bn_rij1 > 10 && $bn_rij1 <= 16){
$rows["row1"][]="<td>" . $bingoNumbers["rij_1"][array_rand($bingoNumbers["rij_1"])] ."</td>";
}
}
foreach($bingoNumbers["rij_2"] as $bn_rij2){
if($bn_rij2 > 20 && $bn_rij2 <= 26){
$rows["row2"][]="<td>" . $bingoNumbers["rij_2"][array_rand($bingoNumbers["rij_2"])] ."</td>";
}
}
foreach($bingoNumbers["rij_3"] as $bn_rij3){
if($bn_rij3 > 30 && $bn_rij3 <= 36){
$rows["row3"][]="<td>" . $bingoNumbers["rij_3"][array_rand($bingoNumbers["rij_3"])] ."</td>";
}
}
foreach($bingoNumbers["rij_4"] as $bn_rij4){
if($bn_rij4 > 40 && $bn_rij4 <= 46){
$rows["row4"][]="<td>" . $bingoNumbers["rij_4"][array_rand($bingoNumbers["rij_4"])] ."</td>";
}
}
foreach($bingoNumbers["rij_5"] as $bn_rij5){
if($bn_rij5 > 50 && $bn_rij5 <= 56){
$rows["row5"][]="<td>" . $bingoNumbers["rij_5"][array_rand($bingoNumbers["rij_5"])] ."</td>";
}
}
foreach($bingoNumbers["rij_6"] as $bn_rij6){
if($bn_rij6 > 60 && $bn_rij6 <= 66){
$rows["row6"][]="<td>" . $bingoNumbers["rij_6"][array_rand($bingoNumbers["rij_6"])] ."</td>";
}
}
echo "<table>";
foreach($rows as $row){
echo "<tr>";
foreach($row as $r){
echo $r;
}
echo "</tr>";
}
echo "</table>";
}//END OF FUNCTION
printBingocard();
?>
</body>
</html>
答案 0 :(得分:3)
为什么不使用shuffle()
?就像现在一样,你永远不会检查array_rand()
产生重复的数字,所以是的 - 你会得到愚蠢的。
效率更高:
foreach(array_keys($bingoNumbers) as $key) {
shuffle($bingoNumbers[$key]);
}
一个 SINGLE 循环,每个子数组都被洗牌,没有重复,远远少于代码。
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
foreach(array_keys($bingoNumbers) as $key)
shuffle($bingoNumbers[$key]);
echo "<table border='1'>";
foreach($bingoNumbers as $v)
echo "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
echo "</table>";
&#13;
如果您只想要一个6x6网格,只需要array_slice()
您的子阵列,例如
$bingoNumbers = array(
"rij_1" => array(10,11,12,13,14,15,16,17,18,19),
"rij_2" => array(20,21,22,23,24,25,26,27,28,29),
"rij_3" => array(30,31,32,33,34,35,36,37,38,39),
"rij_4" => array(40,41,42,43,44,45,46,47,48,49),
"rij_5" => array(50,51,52,53,54,55,56,57,58,59),
"rij_6" => array(60,61,62,63,64,65,66,67,68,69)
);
foreach(array_keys($bingoNumbers) as $key)
shuffle($bingoNumbers[$key]);
echo "<table border='1'>";
foreach($bingoNumbers as $v)
echo "<tr><td>" . implode("</td><td>", array_slice($v, 0, 6)) . "</td></tr>";
//^^^^^^^^^^^^ Just slice your sub array
echo "</table>";
&#13;