php - 设置随机播放,不重复

标签 php arrays random shuffle

我有一个闪存卡数组,使用随机播放我输出了 15 张独特的卡片,每张 3 张用于 5 个不同的类别。

我想做的是在同一个网页上为大约十几个人创建这些卡片集,但我无法弄清楚的部分是如何制作它以便每个完整的集都是独一无二的并且不会重复给任何其他用户的集合。

带有简短说明的简短代码示例对我最有帮助。

这是我根据需要修改的代码。确实没有太大变化。

<?php
/* original source:
 * 3d10-engine.php
 * by Duane Brien
 */

if (empty($_POST)) {
for ($i = 1; $i < 16; $i++) {
    $numbers['ALL'][] = $i;
}

$picks = array();


$letters = array ('ALL');
foreach ($letters as $letter) {
    for ($i = 0;$i < 10;$i++) {
        shuffle($numbers[$letter]);
        $chunks = array_chunk($numbers[$letter], 5);
        $cards[$i][$letter] = $chunks[0];
        if ($letter == 'N') {
            $cards[$i][$letter][2] = '  '; // Free Space
        }

    }
    foreach ($numbers[$letter] as $number) {
        $balls[] = $letter.$number;
    }
    shuffle($balls);
}

$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);

} else {
$cards = unserialize($_POST['cardsstr']);
$balls = unserialize($_POST['ballsstr']);
$picks = unserialize($_POST['picksstr']);
array_unshift($picks, array_shift($balls));

echo "<h1>Just Picked: " . $picks[0] . "</h1>";

$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
}

?>
Picks : <?php echo implode(',', $picks) ?>
<form method='post'>
<input type='hidden' name='cardsstr' value='<?php echo $cardsstr ?>' />
<input type='hidden' name='ballsstr' value='<?php echo $ballsstr ?>' />
<input type='hidden' name='picksstr' value='<?php echo $picksstr ?>' />
<input type='submit' name='cards' value='next number' />
</form>
<a href="3d10-engine.php">Start Over</a>
<?php

foreach ($cards as $card) {
echo "<table border='1'>";
echo "<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>";
for ($i = 0; $i < 5; $i++) {
    echo "<tr><td>" . $card['B'][$i] . "</td><td>" .$card['I'][$i]  . "</td><td>" . $card['N'][$i] . "</td>";
    echo "<td>" . $card['G'][$i] . "</td><td>" . $card['O'][$i] . "</td></tr>";
}
echo "</table>";
}

?>

最佳答案

由于您在每组中有更多选项,随机选择足以获得独特的最终结果。 我的意思是不要让这件事变得更复杂。

试试这个例子

<?php
//Initialize your 5 sets here
$numbers['B'] = range(1,15);
$numbers['I'] = range(16,30);
$numbers['N'] = range(31,45);
$numbers['G'] = range(45,60);
$numbers['O'] = range(61,75);

//My Assumption is you to pick 3 from each 

while(TRUE){
 $rand = rand(0,5);
 if(count($numbers_B) < 3 && !in_array($numbers['B'][$rand]){
   $numbers_B[] = $numbers['B'][$rand];
 }

 $rand = rand(0,5);
 if(count($numbers_I) < 3 && !in_array($numbers['I'][$rand]){
   $numbers_I[] = $numbers['I'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_N) < 3 && !in_array($numbers['N'][$rand]){
   $numbers_N[] = $numbers['N'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_G) < 3 && !in_array($numbers['G'][$rand]){
   $numbers_G[] = $numbers['G'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_O) < 3 && !in_array($numbers['O'][$rand]){
   $numbers_O[] = $numbers['O'][$rand];
 }

if( count($numbers_B) == 3 && count($numbers_I) == 3 && count($numbers_N) == 3 && 

count($numbers_G) == 3 && count($numbers_O) == 3 ){
  break;
 }
    }
    $result = $numbers_B + $numbers_I + $numbers_N + $numbers_G + $numbers_O; ?>

这里 $result 值应该是唯一的,我认为集合的数量是恒定的。如果它是动态的,则用二维数组尝试相同的逻辑。

关于php - 设置随机播放,不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14122827/

相关文章:

javascript - 如何在纯 JavaScript(不是 Node.js)中生成一个范围内的随机 BigInt?

c++ - c++11 中只有 std::mt19937 的实例重复值

java - 如何编写一个由 5 个整数组成的随机数组,但不将 0 算作整数?

php - 如何将传单多边形框放大 5%?

java - 冒泡排序再次返回相同的数组?

php - 快速处理 PHP 响应

c - 在 2d 10x10 表中打印值,在 C 中

javascript - getElementsByClassName 返回一个数组,但一些成员未定义

php - ffmpeg 使用 php 变量将 wav 转换为 mp3

php - 多次使用绑定(bind)参数