3
\$\begingroup\$

This program generates 6 random numbers between 0 and 45 and the numbers cannot be reapeated. Can this code be improved?

$numbers = [];

$i = 1;

while($i <= 6)
{
    $number = mt_rand(0, 45);

    if(!in_array($number, $numbers))
    {
        array_push($numbers, $number);
        $i++;
    }
}

sort($numbers);

echo implode(" - ", $numbers);
\$\endgroup\$
2
\$\begingroup\$

Your algorithm is functional, but is not necessarily very efficient, and it will collide more, and more often if the count of numbers increases.

It's generally neater to start off with a unique set of numbers, and to extract a random selection of those numbers, instead of selecting random numbers, and testing for uniqueness.

PHP makes this relatively neat, because it has a built-in shuffle function (that's essentially doing a Fisher-Yates shuffle using the same random system that you are).

So, get an array of the unique values, shuffle it, take a selection of it, and sort the result:

$count = 6;
$highball = 45;
$numbers = range(0, $highball);
shuffle($numbers);
$drawn = array_slice($numbers, - $count);
sort($drawn);

Note, using named variables instead of constants makes it clearer what you are doing.

See this running on ideone: https://ideone.com/1Hh0y8

\$\endgroup\$
  • \$\begingroup\$ Generating 6 distinct numbers out of 45 is not likely to need more random numbers than shuffling a 45-element array, even with collisions. And I doubt that 6 and 45 will change by much, for a typical lottery. \$\endgroup\$ – 200_success Mar 30 '16 at 1:01
  • \$\begingroup\$ @200_success - agreed, and although my first sentence mentions efficiency, that is about the algorithm, the real point is about neatness, and function of the code. 2 levels of nesting are removed with the above too, and excluding the magic-number declarations, it's now just 4 lines of code. \$\endgroup\$ – rolfl Mar 30 '16 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.