0

So I am working on my Final Project for a web application development class I'm taking and I am creating a Powerball lottery generator. For this to work, the White ball numbers cannot be duplicated. Here is how my code is looking so far:

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

The issue with this code is that there is a chance that variables 'a' through 'e' have a chance of being duplicate numbers. What code could I use to ensure that none of the variables 'a' through 'e' are the same? I thought of doing something like:

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

But that is just too much work and I always try to find the most efficient ways to write code. I don't want to have to write more code than I need to. Any assistance would be greatly appreciated. Thank you in advance!

Escanor
  • 7
  • 1

5 Answers5

2

You could generate the numbers this way:

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

Also take a look at Generating random numbers without repeats

caylee
  • 921
  • 6
  • 12
0

Add them in an array and check for uniqueness:

<?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 />";
    }
asiviero
  • 1,225
  • 10
  • 16
0

While loop the random generation of numbers and check for duplicates on the fly.

Test it here: https://3v4l.org/odOqb
I have changed the random numbers to a smaller size to see if it does create duplicates.
But I have not seen any.

<?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 />";
};
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

To make it as efficient as possible you do not want to have to generate a new set of numbers each time therefore if a duplicate appears you would just want to re-pick for that letter right away.

To do this you can add the elements to an array and search through it after each letter to make sure its a unique number. This is done through the utilization of the for loop, while loop, and check variable.

<?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 />";
}
  • Just a tip. If you repeat the same code over and over the way you do, you can make that a function. Cleaner and more efficient code. – Andreas May 31 '17 at 08:22
-1

Below code is for 6/49 Canada lottery

<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>
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • 3
    Welcome to Stack Overflow! Please provide an explanation with your answers, as standalone code snippets might not be self explanatory and not as helpful. Thanks! – dferenc Dec 17 '17 at 16:39