Php 确保变量不相等(彩票代码)
因此,我正在参加一个web应用程序开发课程的最后一个项目,我正在创建一个Powerball彩票生成器。要使其工作,不能复制白色球编号。以下是我的代码到目前为止的情况:Php 确保变量不相等(彩票代码),php,html,Php,Html,因此,我正在参加一个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));
<?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 />";
};
?>
但这实在是太多的工作了,我总是试图找到最有效的方法来编写代码。我不想写太多的代码。如蒙协助,将不胜感激。提前谢谢你 将它们添加到数组中并检查唯一性:
<?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 />";
}
您可以通过以下方式生成数字:
$arr = range(1, 69);
shuffle($arr);
$a = $arr[0];
$b = $arr[1];
$c = $arr[2];
$d = $arr[3];
$e = $arr[4];
在循环随机生成的数字并动态检查重复的数字时,也可以查看一下 在这里测试它: 我已经将随机数更改为较小的大小,以查看它是否会创建重复项。 但是我没有看到
<?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 />";
};
为了尽可能地提高效率,您不希望每次都生成一组新的数字,因此,如果出现重复的数字,您只需要立即重新选择该字母 为此,您可以将元素添加到数组中,并在每个字母后搜索,以确保其为唯一数字。这是通过使用for循环、while循环和check变量来实现的
<?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 />";
}
下面的代码是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>
很简单。喜欢!只是一个提示。如果您以这种方式反复重复相同的代码,您可以将其作为一个函数。更干净、更高效的代码。欢迎使用堆栈溢出!请在回答时提供解释,因为独立的代码片段可能不是自解释的,也没有多大帮助。谢谢
<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>