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

1 Answer

0 votes
import random

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

It uses:
    - random.sample() for selecting distinct numbers without duplicates
    - random.randint() for the single power number
    - clean functions and idiomatic Python style
"""

def pick_distinct_numbers(count, max_value):
    """
    pick_distinct_numbers(count, max_value):
    Returns 'count' distinct random numbers from the range [1..max_value].

    Algorithm:
        - Use random.sample(), which:
            * guarantees distinct numbers
            * is efficient
            * avoids manual duplicate checks
    """
    return random.sample(range(1, max_value + 1), count)


def pick_power_number(max_value):
    """
    pick_power_number(max_value):
    Returns a single random number in the range [1..max_value].
    """
    return random.randint(1, max_value)


def main():
    main_count = 6
    main_max = 37
    power_max = 7

    # Generate main numbers (distinct)
    main_numbers = pick_distinct_numbers(main_count, main_max)
    main_numbers.sort()  # sort for nicer output

    # Generate power number
    power_number = pick_power_number(power_max)

    # Output results
    print("Main numbers (6 out of 37):", *main_numbers)
    print("Power number (1 out of 7):", power_number)


if __name__ == "__main__":
    main()


"""
run:

Main numbers (6 out of 37): 5 15 16 28 29 33
Power number (1 out of 7): 1

"""

 



answered Jul 28 by avibootz

Related questions

...