# Generate 6 set of unique lottery numbers (6 in a row and six rows). Check if they do not repeat but this is optional

Hi guys,

I have the following code:

``````\$numbers = range(1, 49);
shuffle(\$numbers);
\$numbers = array_slice(\$numbers, 0, 6);

print "Winning numbers: ";
foreach (\$numbers as \$number => \$winning_numbers) {
print \$winning_numbers . " ";
}
``````

It generates just one set of 6 numbers - i would like to repeat it 6 times and optionally check if the numbers do not repeat.

So if you have a dupe, do you want to select all six over again, or just the dupe?

Hi @DaveMaxwell,

I am not sure I undersdand
I basically would like to have an output similar to:

``````12 43 43 23 45 7
29 44 22 18 33 8
etc (six rows)
``````

You need to be more specific about what you want exactly.

For example. you could `array_slice() \$numbers` at different offsets.
This would ensure that each is unique and there would not be any repeats.

• BUT- it would also mean that more then one could not have a repeated number even if it had a different combination.

i.e.
1, 2, 3, 4, 5, 6
6, 8, 9, 10, 11, 12
13, 14, 15, 16, 17, 18
vs.
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 12
1, 2, 3, 4, 5, 18

In any case, I think it may be a good idea to instead of going directly to a `print` the number groups were put into an array and subsequent groups checked for uniqueness before adding them to the array

Easiest way would be to use recursion, and strip off the previously attained numbers

``````\$baseArray = array();
LotteryNumbers(\$baseArray, 6);

function LotteryNumbers(\$previousNumbers, \$setLimit) {
\$numberRange = range(1, 49);

// strip off any previous numbers, shuffle and get first six
\$numbers = array_diff(\$numberRange, \$previousNumbers);
shuffle(\$numbers);
\$numbers = array_slice(\$numbers, 0, 6);

if (count(\$previousNumbers) == 0) print "Winning numbers: " . PHP_EOL;
foreach (\$numbers as \$number => \$winning_numbers) {
print \$winning_numbers . " ";
}
print PHP_EOL;

// combine previous array and this one to see how many numbers have been found
\$allNumbers = array_merge(\$numbers, \$previousNumbers);
if ((count(\$allNumbers) / 6) < 6) {
LotteryNumbers(\$allNumbers, \$setLimit);
}
}
``````

Sample Output

``````Winning numbers:
4 18 5 20 2 25
46 17 1 24 47 13
29 15 11 12 36 28
37 33 32 22 27 9
49 40 19 16 10 38
34 43 44 41 42 23
``````
1 Like

far easier than that is an alternative already suggested:

``````for (\$i = 0; \$i < 6; \$i++) {
\$numbers = array_slice(\$numbers, \$i*6, 6);
print "Winning numbers: ";
foreach (\$numbers as \$number => \$winning_numbers) {
print \$winning_numbers . " ";
}
print PHP_EOL;
}
``````
1 Like

While this sounds like a homework assignment, it is a good opportunity to actually create code using the generator.

``````function lottery(\$getTickets, \$range, \$ticketDigits) {
\$previous = array();

for (\$num=0;\$num < \$getTickets;) {
shuffle(\$range);

\$numbers = array_slice(\$range, mt_rand(0,count(\$range)-\$ticketDigits), \$ticketDigits);
sort(\$numbers);
\$ticket = implode(' ', \$numbers);

if (!isset(\$previous[\$ticket])) {
\$previous[\$ticket] = true;
++\$num;

yield \$ticket;
}
}
}

foreach (lottery(6, range(1, 49), 6) as \$ticket) {
echo \$ticket.'<br />';
}
``````

The problem with this code is that none of the tickets will have some of the similar numbers, since the shuffle happen before the for loop.

EDIT: Fixed a bug in the generator, making certain we only count the ticket when it is unique.

1 Like

Youâ€™d have to put this in a different array, or the 2nd time you try to run it, youâ€™ll get unexpected results since the array will have been cut down to the six elements from the first sliceâ€¦

1 Like

Not sure if the double 43, is a mistake or if it is possible that each of the six digits can be the same number?

If, this is how it should work, all you need to do with the generator code above, is make the range contain six times the values 1-49.

1 Like

Oops - so replacing the \$i*6 with 0 would rectify that without needing to use a new array

1 Like

Thank you so much guysâ€¦ different approaches and eye-opening #muchappreciated

Noooo, because that would still only leave six elements left in the array. The first number is the starting element in the array, the second is how many to slice off.

But if you put it into a new element, you can continually slice off the same array without having to regen the numbers nor check for dupes. So, slightly altering your approach, this would workâ€¦

``````\$numbers = range(1, 49);
shuffle(\$numbers);

for (\$i = 0; \$i < 6; \$i++) {
print "Winning numbers: "
\$tempNumbers = array_slice(\$numbers, \$i*6, 6);   // will pull elements 0-5, 6-11, 12-17, etc.....
foreach (\$tempNumbers as \$number => \$winning_numbers) {
print \$winning_numbers . " ";
}
print PHP_EOL;
}
``````

Though as a caveat, this approach will only randomize the list of numbers once, so itâ€™s like dealing blackjack without shuffling the cards each timeâ€¦it could be gamed

Thatâ€™s basically what I thought I had - thatâ€™s what happens when I donâ€™t check what I have written properly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.