确保没有变量相等(彩票代码)

时间:2017-05-30 19:41:38

标签: php html

所以我正在为我正在进行的Web应用程序开发课程的最终项目工作,我正在创建一个强力球彩票生成器。为此,白球数不能重复。以下是我的代码到目前为止的样子:

<?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 />";
};
?>

此代码存在的问题是变量有可能会出现变形问题。通过&#39; e&#39;有机会成为重复的数字。我可以使用什么代码来确保没有任何变量&#39; a&#39;通过&#39; e&#39;是相同的?我想做类似的事情:

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

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

5 个答案:

答案 0 :(得分:2)

您可以通过这种方式生成数字:

$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

答案 1 :(得分:0)

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

<?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 />";
    }

答案 2 :(得分:0)

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

在此测试: 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 />";
};

答案 3 :(得分:0)

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

要执行此操作,您可以将元素添加到数组中,并在每个字母后搜索它以确保其唯一的数字。这是通过使用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 />";
}

答案 4 :(得分:-1)

以下代码适用于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>
相关问题