Law of large numbers in python

The absolute reason why you can't make money by gambling is the "law of large numbers". From Kotobank below

The law of large numbers is a theorem in which the number of occurrences of an event approaches a theoretical value by repeating many trials, such as the number of appearances being close to 1/2 by repeating coin throwing many times.

Let's demonstrate this with python.

large_num.py


#Random number generator library
import random
#Library for math calculations
import math

#Set the number of trials
max_try = int(input('Enter the maximum number of attempts (10 is the power of the input value)) :\n'))
#Set pattern
pattern = int(input('Please enter what fraction you want to calculate: \n'))
#Calculate theoretical probabilities
theory = (1 / pattern) * 100
print(theory)

print('\n{0}The probability of getting 1 out of the number of streets is theoretical{1}%\n'.format(pattern,theory))
print('Calculation start!!\n')

for i in range(1,max_try) :
    #Set the number of trials
    try_num = pow(10,i)
    #Initialize count
    count = 0

    for x in range(try_num):
        #Random number generation
        dice = random.randint(1,pattern)
        #Judge 1
        if dice == 1 :
            count += 1
    #Calculate probability
    result = (count / try_num) * 100
    #Print the result
    out = '{0}The probability of getting 1 in times is{1}%The difference from the theoretical value{2}%'.format(try_num,result,math.fabs(result - theory))
    print(out)



Recommended Posts

Law of large numbers in python
Project Euler # 13 "Sum of Large Numbers" in Python
Prime numbers in Python
Project Euler # 10 "sum of prime numbers" in Python
Equivalence of objects in Python
Implementation of quicksort in Python
Handle complex numbers in Python
Pixel manipulation of images in Python
Testing with random numbers in Python
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Waveform display of audio in Python
Sort large text files in Python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Is the lottery profitable? ~ LOTO7 and the law of large numbers ~
Various ways to create an array of numbers from 1 to 10 in Python.
Conversion of string <-> date (date, datetime) in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
I searched for prime numbers in python
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
Practical example of Hexagonal Architecture in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Introduction of Python
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Basics of Python ①
Basics of python ①
Programming in python
Constant in python