PHP - 如果两个变量相等,重新随机化一个?

时间:2014-08-04 19:16:32

标签: php random

我是从头开始编写PHP的新手,我正在尝试编写一个简单的小应用程序作为挑战。这个想法是它需要几个.txt文件并从这些文件中的名字中选择一个随机幻想足球队。我确信有一个更短/更简单的方法来做我到目前为止所做的事情,但我所写的内容可以达到一定程度 - 除非我无法让它重新随机化一个玩家同一个球员两次。我哪里错了?

到目前为止,所有代码都在下面。

// Goalkeeper
$f_contents_goalkeepers = file("goalkeepers.txt"); // Load the Goalkeepers Database and add it to an array
shuffle($f_contents_goalkeepers); // Shuffle the contents of the array
$randGoalkeeper = $f_contents_goalkeepers[array_rand($f_contents_goalkeepers)]; // Add a random goalkeeper to the randGoalkeeper variable
$goalkeeper = $randGoalkeeper; // Set the Goalkeeper variable to use the random goalkeeper
//

// Defender
$f_contents_defenders = file("defenders.txt"); // Load the Defenders Database and add it to an array
shuffle($f_contents_defenders); // Shuffle the contents of the array
$randDefender1 = $f_contents_defenders[array_rand($f_contents_defenders)]; // Add a random defender to the randDefender1 variable
$randDefender2 = $f_contents_defenders[array_rand($f_contents_defenders)]; // Add a random defender to the randDefender2

    // If $randDefender2 is the same as $randDefender1, pick another player
    if($randDefender2 == $randDefender1) {
        $randDefender2 = $f_contents_defenders[array_rand($f_contents_defenders)];
    }

$randDefender3 = $f_contents_defenders[array_rand($f_contents_defenders)]; // Add a random defender to the randDefender variable

    // If $randDefender3 is the same as $randDefender2, pick another player
    if($randDefender3 == $randDefender2) {
        $randDefender3 = $f_contents_defenders[array_rand($f_contents_defenders)];
    }

$randDefender4 = $f_contents_defenders[array_rand($f_contents_defenders)]; // Add a random defender to the randDefender variable

    // If $randDefender4 is the same as $randDefender3, pick another player
    if($randDefender4 == $randDefender3) {
        $randDefender4 = $f_contents_defenders[array_rand($f_contents_defenders)];
    }

$defender1 = $randDefender1; // Set the Defender variable to use the random defender
$defender2 = $randDefender2;
$defender3 = $randDefender3;
$defender4 = $randDefender4;

echo "Goalkeeper: " . ' ' . $goalkeeper . ' ' . "<br />";

echo "Defender: " . ' ' . $defender1 . ' ' . "<br />";
echo "Defender: " . ' ' . $defender2 . ' ' . "<br />";
echo "Defender: " . ' ' . $defender3 . ' ' . "<br />";
echo "Defender: " . ' ' . $defender4 . ' ' . "<br />";

3 个答案:

答案 0 :(得分:0)

如果您shuffle()数组,您可以编写算法,而不必担心重新随机化。每次它被改组时,它将以不同的顺序 - 所以你可以使用数组中的第一和第二项,并且它们保证不同。您可以使用$defenders[0]$defenders[1]来获取它们,也可以使用array_pop()数组中的元素。如果您编写的代码事先并不知道您需要多少代码,这种方法特别有用,因为您不必维护计数。只需加载您的防御者列表shuffle(),然后在必要时使用array_pop()获取不同的防御者。

答案 1 :(得分:0)

您可以让array_rand为您选择所有值来解决您的问题。

$defenders = array_rand(f_contents_defenders, 4);

现在回答你的问题,我必须先修复错误。 $f_contents_defenders[array_rand($f_contents_defenders)]语句毫无意义。 array_rand($f_contents_defenders)的结果已经是一个值,因此,无需对其进行索引。

一旦你解决了这个问题,你就得到:     $ randDefender1 = array_rand($ f_contents_defenders); //将随机防御者添加到randDefender1变量中     $ randDefender2 = array_rand($ f_contents_defenders); //将随机防御者添加到randDefender2

为了确保您第二次总能获得另一个,您可以这样做:

while ($randDefender1 == $randDefender2) {
    $randDefender2 = array_rand($f_contents_defenders); // Add a random defender to the randDefender2
}

等等。确保你有超过2名防守者,否则你将陷入困境。

当你去第3和第4时,你需要检查所有以前的防守者:

while ($randDefender3 == $randDefender1 || $randDefender3 == $randDefender2)

如果没有,你可以得到这样的情况:

  • 后卫C
  • 后卫A
  • 后卫C
  • 后卫A

我强烈认为你使用array_rand()抓住一切给你,很难。

答案 2 :(得分:0)

更美妙的方式:

// Goalkeeper
$f_contents_goalkeepers = file("goalkeepers.txt"); // Load the Goalkeepers Database and add it to an array
shuffle($f_contents_goalkeepers); // Shuffle the contents of the array
$randGoalkeeper = $f_contents_goalkeepers[array_rand($f_contents_goalkeepers)]; // Add a random goalkeeper to the randGoalkeeper variable
$goalkeeper = $randGoalkeeper; // Set the Goalkeeper variable to use the random goalkeeper
//

// Defender
$f_contents_defenders = file("defenders.txt"); // Load the Defenders Database and add it to an array
$def_cnt = count($f_contents_defenders)-1;
if($def_cnt < 3)
    die('You need 4 or more defenders');
$def_rand = array();
for($i = 0; $i < 4; ++$i)
{
    $rand_numb = rand(0, $def_cnt);
    while(isset($def_rand[$rand_numb]))
    {
        $rand_numb = rand(0, $def_cnt);
    }
    $def_rand[$rand_numb] = $f_contents_defenders[$rand_numb];
}
$def_rand = array_values($def_rand);
print_r($def_rand);