Challenge LOTO 6 with Python without discipline

Preface

Well, I know that it has already been analyzed and I haven't come to a particularly remarkable conclusion. Still, if I'm studying Python, I'll give it a try.

Development environment

Acquisition of past data

I can't do manual copying, so I decided to scrape it. The last data as of today (20/10/09) is the 1524th.

In LOTO6, 6 numbers and 1 bonus number will be drawn, but this time we will only play with these numbers. The numbers drawn will be 1-43 and will not be duplicated.

scloto6.py


import requests
from bs4 import BeautifulSoup

r = requests.get('http://hogehoge.com/loto6/data/list1/')
soup = BeautifulSoup(r.content, "html.parser")
numbers = soup.find_all('td', class_='w4')

i = 1
with open('index.txt','w') as f:
    for number in numbers:
        s = number.text.replace('\t', '')
        s = s.replace('\n', '')

        if len(s) == 1:
            s = '0' + s
        if (i % 6) != 0:
            s = s + '\t'
        else:
            s = s + '\n'
        f.write(s)
        i += 1

A TAB-delimited text file like this will be generated.

index.txt


02 08 10 13 27 30
01 09 16 20 21 43
01 05 15 31 36 38
16 18 26 27 34 40
09 15 21 23 27 28

Using this as an input file, the following processing is performed.

--Calculate σ from the sum of each time --Count the number of occurrences of each number (1..43) and sort in descending order ――I will buy 5 units ――Two of the six numbers for one unit will be picked up from the top 10 appearances. --The remaining 4 numbers will be randomly picked up from 11..33 excluding the bottom 10 appearances. --6 If the sum of the selected numbers is out of the range of ± σ, start over.

Since the average of the total of each time up to the 1524th time is 131.94 and σ is 28.37, -σ to σ is in the range of 103.57 to 160.31.

The number of data Percentage
-σ~σ 1,042 68.37%
-2σ~2σ 407 95.08%
-3σ~3σ 73 99.87%

In other words, the probability that the total value of the 6 numbers will fall between 104 and 160 is about 2/3. The lottery results such as [1, 2, 3, 4, 5, 6] are 21 in total and deviate from 3σ, so you can almost ignore them (≒ give up if that happens).

pyloto6.py


import math
import random
import numpy as np

appearance_count = {}       #Number of occurrences of each number

for i in range(44):
    appearance_count[i] = 0

sums = []       #Array of total values each time

with open('C:\\Python\\scloto6\\index.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        array = line.split('\t')
        array_n = list(map(int, array))     #Since array is a str array, convert it to an int array
        sums.append(sum(array_n))

        for i in array_n:
           appearance_count[i] = appearance_count[i] + 1

#Sort in descending order by appearance count
sorted_appearance_count = sorted(appearance_count.items(), key=lambda x:x[1], reverse=True)

avg = sum(sums) / len(sums)
print(f"AVG:{avg}")

sigma = np.std(sums)        #standard deviation of sums
sigmalower = avg - sigma
sigmaupper = avg + sigma
print(f"σ:{sigma}({sigmalower}~{sigmaupper})")

for index in [0, 2, 4, 6, 8]:
    while True:
        a = []

        a.append(sorted_appearance_count[index][0])
        a.append(sorted_appearance_count[index + 1][0])

        #The remaining 4 are random(In other words, there is no basis for pickup)
        while len(a) < 6:
            no = random.randint(10, 32)
            value = sorted_appearance_count[no][0]
            if not value in a:
                a.append(value)

        asum = sum(a)

        if sigmalower <= asum and asum <= sigmaupper:
            #The sum of the 6 numbers picked up is-Adopted in the range of σ to σ
            break

    print(f"{asum}{a}")

The output looks like this.

Execution result


AVG:131.93635170603673
σ:28.37024181798044(103.56610988805629~160.30659352401716)
123[6, 38, 22, 26, 11, 20]
128[10, 27, 39, 21, 26, 5]
108[37, 12, 2, 16, 21, 20]
126[24, 15, 39, 35, 5, 8]
129[19, 43, 25, 3, 23, 16]

Afterword

Since it is 200 yen per unit, it is 1000 yen for 5 units, so I have 5 units, but I think I read that 3 units are the best for LOTO 6 somewhere (I forgot the basis ...)

Even if you visualize the appearance numbers for 1524 times, you can not see any tendency, so the physical machine lottery method may not have a strategy. If so, it has been done for 20 years, so it seems that the ancestors have already discovered it. The lottery days are twice every month and Thursday!

Recommended Posts

Challenge LOTO 6 with Python without discipline
Print with python3 without line breaks
Challenge problem 5 with Python: lambda ... I decided to copy without
FizzBuzz with Python3
Scraping with Python
[Python] Challenge 100 knocks! (015 ~ 019)
Statistics with python
Scraping with Python
Python with Go
[Python] Challenge 100 knocks! (030-034)
[Python] Challenge 100 knocks! (006-009)
Twilio with Python
Integrate with Python
Play with 2016-Python
[Python] Challenge 100 knocks! (000-005)
AES256 with python
Tested with Python
[Python] Challenge 100 knocks! (010-014)
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Python] Challenge 100 knocks! (025-029)
python challenge diary ①
Excel with Python
Microcomputer with Python
[Python] Challenge 100 knocks! (020-024)
Cast with python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Python beginners challenge Cpaw CTF Q14 with bubble sort
Scraping with Python (preparation)
Try scraping with Python.
Try to solve the programming challenge book with python3
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
[Small story] Synchronize with Python without multithreading (limited use)
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3