Automatically generate Python Lotto numbers (without duplication)

Asked 2 years ago, Updated 2 years ago, 17 views

I'm an introductory Python student, but it's been blocked for about a week.

I want to create n sets of 6 lotto numbers (set to 3 sets in the code below), but if I make it as below, the computer will have a hard time even if the number of n exceeds 5 million. Is there any way?

import random

import itertools

buy_num_list = [ ]

buy_pool = list(itertools.combinations(list(range(1, 46)), 6))

for i in range(3):

    buy_num_list = random.choice(buy_pool)

    buy_pool.remove(buy_num_list)

    print(buy_num_list)

python

2022-09-20 15:31

1 Answers

import random
import itertools

buyPool = [] # PurchaseList
cntLimit = 10000 #Define how many times to proceed
LottoPool = list(itertools.combinations(list(range(1, 46)), 6)) # LottoPool

print(len(LottoPool), 'is Ready') #LottoPOOL count = 8145060

for cnt in range(cntLimit):
    nowNumber = random.choice(LottoPool)
    If nowNumber not in buyPool: # If it's not the number you purchased,
        print(nowNumber)
        buyPool.append(nowNumber) #Add to purchase list


2022-09-20 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.