【问题标题】:Ensure no variables are equal (Lottery Code)确保没有变量相等(彩票代码)
【发布时间】:2017-10-31 10:10:46
【问题描述】:

所以我正在为我正在学习的网络应用程序开发课程做我的最终项目,并且我正在创建一个强力球彩票生成器。为此,白球号码不能重复。以下是我的代码目前的样子:

<?php

for($x = 1; $x < 6; $x++){

    //set each white ball variable (a through e) to a random number between 1 and 69
    $a = floor((lcg_value() * 69 + 1));
    $b = floor((lcg_value() * 69 + 1));
    $c = floor((lcg_value() * 69 + 1));
    $d = floor((lcg_value() * 69 + 1));
    $e = floor((lcg_value() * 69 + 1));

    //set powerball number variable to a number between 1 and 26
    $f = floor((lcg_value() * 26 + 1));

    //echo all white ball numbers and powerball number
    echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
?>

此代码的问题是变量“a”到“e”有可能成为重复数字。我可以使用什么代码来确保变量“a”到“e”都不相同?我想过做类似的事情:

if($a != $b || $a != $c || $a || $d...){
//echo numbers
}else{
//generate new numbers
};

但这只是太多的工作,我总是试图找到最有效的方法来编写代码。我不想编写比我需要的更多的代码。任何帮助将不胜感激。先感谢您!

【问题讨论】:

    标签: php html


    【解决方案1】:

    您可以这样生成数字:

    $arr = range(1, 69);
    shuffle($arr);
    $a = $arr[0];
    $b = $arr[1];
    $c = $arr[2];
    $d = $arr[3];
    $e = $arr[4];
    

    也可以看看Generating random numbers without repeats

    【讨论】:

    • 很简单。喜欢!
    【解决方案2】:

    将它们添加到数组中并检查唯一性:

    <?php
        for($x = 1; $x < 6; $x++){    
            $unique = false;
            while(!$unique) {
                //set each white ball variable (a through e) to a random number between 1 and 69
                $a = floor((lcg_value() * 69 + 1));
                $b = floor((lcg_value() * 69 + 1));
                $c = floor((lcg_value() * 69 + 1));
                $d = floor((lcg_value() * 69 + 1));
                $e = floor((lcg_value() * 69 + 1));
    
                $numbers = array($a, $b, $c, $d, $e);
                if(count($numbers) == count(array_unique($numbers)) {
                    $unique = true;
                }
            }
            //set powerball number variable to a number between 1 and 26
            $f = floor((lcg_value() * 26 + 1));
    
            //echo all white ball numbers and powerball number
            echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
        }
    

    【讨论】:

      【解决方案3】:

      同时循环随机生成数字并动态检查重复项。

      在这里测试它: https://3v4l.org/odOqb
      我已将随机数更改为较小的大小,以查看它是否会产生重复。
      但是我没有看到。

      <?php
      $arr =array();
      for($x = 1; $x < 6; $x++){
      
      //set each white ball variable (a through e) to a random number between 1 and 69
      While (count($arr) != 5){
      
          $arr[] = floor((lcg_value() * 6 + 1));
          $arr = array_unique($arr);
      }
      
      //set powerball number variable to a number between 1 and 26
      $f = floor((lcg_value() * 26 + 1));
      
      Var_dump($arr);
      //echo all white ball numbers and powerball number
      //echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
      };
      

      【讨论】:

        【解决方案4】:

        为了使其尽可能高效,您不希望每次都生成一组新数字,因此如果出现重复,您只想立即重新选择该字母。

        为此,您可以将元素添加到数组中,并在每个字母之后搜索它以确保其唯一编号。这是通过使用 for 循环、while 循环和检查变量来完成的。

        <?php
        
        for($x = 1; $x < 6; $x++) {
            //set each white ball variable (a through e) to a random number between 1 and 69
            $uniqueNumbers = array();
            $check = true;
            $a = floor((lcg_value() * 69 + 1));
            array_push($uniqueNumbers, $a);
            while ($check) {
                $check = false;
                $b = floor((lcg_value() * 69 + 1));
                foreach ($uniqueNumbers as $element) {
                    if ($b == $element) {
                        $check = true;
                    }
                }
            }
            array_push($uniqueNumbers, $b);
            $check = true;
        
            while ($check) {
                $check = false;
                $c = floor((lcg_value() * 69 + 1));
                foreach ($uniqueNumbers as $element) {
                    if ($c == $element) {
                        $check = true;
                    }
                }
            }
            array_push($uniqueNumbers, $c);
            $check = true;
        
            while ($check) {
                $check = false;
                $d = floor((lcg_value() * 69 + 1));
                foreach ($uniqueNumbers as $element) {
                    if ($d == $element) {
                        $check = true;
                    }
                }
            }
            array_push($uniqueNumbers, $d);
            $check = true;
        
            while ($check) {
                $check = false;
                $e = floor((lcg_value() * 69 + 1));
                foreach ($uniqueNumbers as $element) {
                    if ($e == $element) {
                        $check = true;
                    }
                }
            }
            array_push($uniqueNumbers, $e);
        
            //set powerball number variable to a number between 1 and 26
            $f = floor((lcg_value() * 26 + 1));
        
            //echo all white ball numbers and powerball number
            echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
        }
        

        【讨论】:

        • 只是小费。如果你一遍又一遍地重复相同的代码,你可以把它变成一个函数。更简洁、更高效的代码。
        【解决方案5】:

        以下代码适用于 6/49 加拿大彩票

        <body bgcolor="gold"><font size="9"></font>
        <pre>
        <?php
        // Code by bhupinder Deol . modify according to needs
        for ($i=0;$i<=10;$i++) {
        for ($x=0;$x<=5;$x++) {
            $rand[$x]=rand(1,49);
        }
        asort($rand);
        $result = array_unique($rand);
        $count = count($result);
        if ($count == 6) {
        print_r($rand);
        $x=0;
        }
        else
        {
            echo "same numbers in array";
            --$x;
        }
        }
        ?>
        </pre>
        </body>
        

        【讨论】:

        • 欢迎来到 Stack Overflow!请在您的答案中提供解释,因为独立代码 sn-ps 可能无法自我解释且没有帮助。谢谢!
        猜你喜欢
        • 2014-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-04
        • 2021-07-08
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多