Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to generate random lottery numbers for 6 numbers out of 37 and 1 power number out of 7 in PHP

1 Answer

0 votes
/*
    This program generates random lottery numbers for:
        - 6 distinct numbers out of 37
        - 1 distinct number out of 7 (power number)

    It uses:
        - random_int() for secure random number generation
        - shuffle() to efficiently pick unique numbers
        - clean functions and idiomatic PHP style
*/

/*
    pickDistinctNumbers($count, $max):
    Generates $count distinct random numbers from the range [1..$max].

    Algorithm:
        - Create an array containing all numbers 1..$max
        - Shuffle the array using shuffle()
        - Take the first $count numbers

    This guarantees:
        - all numbers are unique
        - uniform randomness
        - no duplicate checks needed
*/
function pickDistinctNumbers(int $count, int $max): array {
    $numbers = range(1, $max);   // full range
    shuffle($numbers);           // Fisher–Yates under the hood
    
    return array_slice($numbers, 0, $count);
}

/*
    pickPowerNumber($max):
    Returns a single random number in the range [1..$max].
*/
function pickPowerNumber(int $max): int {
    return random_int(1, $max);
}

// Main program
$mainCount = 6;
$mainMax   = 37;
$powerMax  = 7;

// Generate main numbers
$mainNumbers = pickDistinctNumbers($mainCount, $mainMax);
sort($mainNumbers); // sort for nicer output

// Generate power number
$powerNumber = pickPowerNumber($powerMax);

// Output results
echo "Main numbers (6 out of 37): " . implode(' ', $mainNumbers) . PHP_EOL;
echo "Power number (1 out of 7): $powerNumber" . PHP_EOL;



/*
run:

Main numbers (6 out of 37): 5 10 22 25 32 37
Power number (1 out of 7): 4

*/

 



answered Jul 28 by avibootz

Related questions

...