php - 生成随机数组

标签 php

快速了解我自己的背景。这是我第一次用 PHP 编码。我拥有计算机信息系统学位,在大学(大约 15 年前)学习了 C++、VB、Cobol 和 Java,但此后就没有真正使用过它们。当我了解到这一点时,一些东西又回到了我的脑海中。

我正在尝试模拟集换式纸牌游戏中打开随机纸牌包的情况。最终结果是打印出 4 页。每页会列出12个卡号,以及卡上的所有信息。

这就是我计划做的事情:

第一步: 生成 180 个不同范围的随机数。每个数字代表游戏中的一张牌。

  • 范围 1 = 35-74(116 个数字)
  • 范围 2 = 75-106(46 个数字)
  • 范围 3 = 107-134(16 个数字,无重复)
  • 范围 4 = 135-142(2 个数字,无重复)

第二步: 将 180 个数字分解为 90 对。

第三步: 从 90 对中,将它们分成 4 组,每组 6 对。

第四步: 从 4 组 6 对中,列出每个号码的卡信息,并制作 4 页可打印页,每组 1 页。

我已经创建了 180 个随机数。我仍在努力获取唯一的号码。我尝试过为我需要的数字创建数组,但它们都不起作用。这是我最后的工作代码,它将生成我需要的 180 个数字,但是,需要修复范围 3 和 4 以不允许重复。

我目前的编码方式,它只是在屏幕上显示数字。我应该将它们存储在数组中吗?我只是完全以错误的方式解决问题吗?

<?php

// generate 116 common cards
echo "Commons: " . '<br />';
for ($commonfeed = 0; $commonfeed < 116; $commonfeed++) {
echo mt_rand(35, 74). '<br />';
}

// generate 46 uncommon cards
echo "Uncommons: " . '<br />';
for ($uncommonfeed = 0; $uncommonfeed < 46; $uncommonfeed++) {
echo mt_rand(75, 106). '<br />';
}

// generate 16 rare cards
echo "Rares: " . '<br />';
for ($rarefeed = 0; $rarefeed < 16; $rarefeed++) {
echo mt_rand(107, 134). '<br />';
}

// generate 2 super rare cards
echo "Super Rares: " . '<br />';
for ($superrarefeed = 0; $superrarefeed < 2; $superrarefeed++) {
echo mt_rand(135, 142). '<br />';
}

?>

最佳答案

以下是您可以尝试的解决方案:

$cards = array();
// get cards per range

for($i = 0; $i < 116; $i++) {
    // range 1:
    $cards[] = mt_rand(35, 74); 
    // for the fun, let's also do range 2:
    if($i < 46) {
        $cards[] = mt_rand(75, 106);
    }
}

// range 3: (and range 4)
$rare = array();
$superrare = array();
for ($i = 107; $i <= 134; $i++) {
    $rare[] = $i;
    // range 4:
    if ($i <= 114) {
        $superrare[] = $i + 28;
    }
}
shuffle($rare);
shuffle($superrare); // not the best choice of randomness (check: http://stackoverflow.com/questions/5694319/how-random-is-phps-shuffle-function)

$cards = array_merge($cards, array_slice($rare, 0, 16));
$cards = array_merge($cards, array_slice($superrare, 0, 2));

// shuffle everything one more time since cards have been added randomly 
// to the deck
shuffle($cards);

// now when we have everything in $cards - 180 of random cards, we can
//    pack them
$pack1 = array_slice($cards, 0, 90);
$pack2 = array_slice($cards, 90, 90);

// always picking first n cards because they are all shuffled
$player1Drafted = array_slice($pack1, 0, 48);
$player2Drafted = array_slice($pack2, 0, 48);

// print it all
print_r(array('Player1' => $player1Drafted, 
              'Player2' => $player2Drafted));

最后,我不确定我是否猜对了起草过程,但在我看来,随机化是最大的问题,我解决了它。再说一遍,如果您认为 shuffle 不够好,可以采取不同的方式,但那是另一个故事了;)

关于php - 生成随机数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607776/

相关文章:

php - 编码 HTML 时,浏览器并不总能检测到更改

php - 如何根据内容和使用 PHP/JavaScript 通过 MySQL 查询生成的 : Conditionally format a <td>,

PHP 和 SQL INSERT JOIN 2 个表,条件错误

php - 如何在 YII2 模型上手动更改主键

php - 带有 MySQL 数据库的 PHP Yii 框架上的应用程序是否可以处理 20,000 名员工的 ERP 解决方案?

php - 如何处理对 PHP 准备的 SELECT EXISTS 查询的响应?

php - 尽管满足条件,但如果在条件语句中调用 return,则函数返回 null。返回条件之外的期望值

php - 如何摆脱用 PHP 创建的表格顶部的额外换行符?

php - 查询没有返回结果

php - HTML 表单完成两个 Action ?验证然后通过一个提交按钮重定向