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

问题描述 投票:0回答:5

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

<?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
5个回答
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


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

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

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

-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>
© www.soinside.com 2019 - 2024. All rights reserved.