본문 바로가기

카테고리 없음

파이썬으로 확률 계산하고 로또번호 생성하기

728x90
반응형

로또번호를 예측하는 건 무의미하다고 말하지만

그래도 하고 싶었다.

 

CSV 파일로 역대 당첨 번호를 저장한다.

이를 읽어 낸다.

# read sequences from file
lotto_df = pd.read_csv('sequences.csv', header=None)

 

 

시퀀스를 리스트 형태로 변환하고, 모든 번호를 하나의 리스트로 펼칩니다.

 

# convert sequences to list of lists
sequences = lotto_df.values.tolist()

각 번호의 빈도수를 계산하고, 각 번호의 확률을 계산합니다.

 

# flatten sequences into a single list of numbers
numbers = [num for seq in sequences for num in seq]

# count frequency of each number
num_counts = {num: numbers.count(num) for num in set(numbers)}

확률이 높은 상위 10개의 번호를 선택합니다. 시퀀스를 문자열 형태로 변환하고, 각 시퀀스의 확률을 계산합니다.

# calculate probability of each number
num_probs = pd.DataFrame({'number': list(num_counts.keys()), 'probability': [count/len(numbers) for count in num_counts.values()]})

# sort numbers by probability and select top 10
top_nums = num_probs.sort_values(by='probability', ascending=False)['number'][:10]

# convert sequences to string format for easy comparison
seq_strings = ['\t'.join(str(num) for num in seq) for seq in sequences]

# calculate probability of each sequence
seq_probs = {seq_string: sum(1 for num in top_nums if str(num) in seq_string)/len(top_nums) for seq_string in seq_strings}

확률이 높은 상위 10개의 시퀀스를 선택합니다.

# sort sequences by probability and select top 10
top_seqs = sorted(seq_probs, key=seq_probs.get, reverse=True)[:10]

# print top 10 most probable sequences
for i, seq_string in enumerate(top_seqs):
    print(f'{i+1}: {seq_string}\nProbability: {seq_probs[seq_string]:.3f}\n')

 

결과를 슬랙 채널로 보내기. 슬랙 봇으로 메시지 보내는 설정은 다른 포스팅에서...

 

class slack:
    def __init__(self, token, channel):
        self.token = token
        self.channel = channel

    def message(self, message):
        response = requests.post("https://slack.com/api/chat.postMessage",
                                 headers={"Authorization": "Bearer " + self.token},
                                 data={"channel": self.channel, "text": message}
                                 )

app_token = 'app token'  # App Token
channel = '#Slack Channel Name'  # Slack Channel Name
slackBot = slack(app_token, channel)

# build message with top 10 sequences
message = 'lotto number Top 10 for this week:\n' 
for i, seq_string in enumerate(top_seqs):
    message += f'{i+1}: {seq_string}\nProbability: {seq_probs[seq_string]:.2f}\n'

# send message to Slack channel
try:
    response = slackBot.message(message)
    print("Message sent: ", message)
except SlackApiError as e:
    print("Error sending message: {}".format(e))

 

이렇게 만들어서 몇주간 만원씩 해봤다. 5천원 몇번 당첨되기는 했는데 하나도 맞지 않는 경우가 더 많다...

자신만으 방법으 더하고 더해서 다들 당첨되시기를

 

그냥  랜덤하게 숫자 6개 뽑는 것과 크게 다르지 않을수도 있다 ㅎㅎ

 

아래는 그냥 숫자 1~45까지 숫자 중 6개를 랜덤하게 뽑는 코드. 어떤 코드가 1등 당첨 숫자를 뽑아낼지는 모름 ㅎㅎ

import random

def generate_lotto_numbers():
    numbers = list(range(1, 45))
    lotto_numbers = random.sample(numbers, 6)
    return sorted(lotto_numbers)

for i in range(5):
    seq = generate_lotto_numbers()
    print(seq)


반응형