파이썬 로또 번호 자동생성(중복없이)

조회수 911회

파이썬 입문자인데 한 1주일째 막혀있습니다.

로또번호 6개씩 n 세트(밑에 코드에서는 3세트로 설정)를 중복없이 생성하고 싶은데 아래와 같이 만들면 n 수가 500만 넘어가도 컴퓨터가 힘들어합니다. 방법이 없을까요?

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)

1 답변

  • import random
    import itertools
    
    buyPool = []  # 구입 목록
    cntLimit = 10000  # 몇회 까지 진행시킬지 정의
    LottoPool = list(itertools.combinations(list(range(1, 46)), 6))  # 로또 POOL
    
    print(len(LottoPool), 'is Ready')  # 로또 POOL 갯수 = 8145060
    
    for cnt in range(cntLimit):
        nowNumber = random.choice(LottoPool)
        if nowNumber not in buyPool:  # 기 구입한 번호가 아니면
            print(nowNumber)
            buyPool.append(nowNumber)  # 구입 목록에 추가
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)