import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter as FF
import numpy as np
import pandas as pd
from random import choice as RC
import seaborn as sns
circle = list(range(1, 21))
#Given a table and an index to start with, proceed to eliminate every 2nd or 3rd position after.
def eliminate(table) :
#Whom to eliminate
skip = RC([2,3])
end = len(table)
#Eliminate a player, shift 1 or 2 players before eliminated to end of table.
if len(table) > 3 :
table = table[skip : end] + table[0:skip-1]
elif len(table) == 3:
#[1,2,3] -> [1,2]
if skip == 3:
del(table[2])
#[1,2,3] -> [3,1]
else :
table = table[2:3] + table[0:1]
elif len(table) == 2 :
#[1,2] -> [2]
if skip == 3:
del(table[0])
#[1,2] -> [1]
else :
del(table[1])
if len(table) != 1:
table = eliminate(table)
return(table)
def MonteCarlo(n) :
last = []
for i in range(n) :
num = eliminate(circle)
last += num
return(last)
last = MonteCarlo(100000000)
steps = set(last)
#Plot.
sns.set()
fig = plt.figure(figsize = (16, 10))
ax = fig.add_subplot()
#Histogram.
bars = plt.hist(x = last,
#Number of bins.
bins = np.arange(len(steps) + 1) + min(steps) - 0.5,
align = "mid",
color = '#1F968B')
#Labels.
for i in range(len(steps)):
plt.text(bars[1][i] + 0.5,
bars[0][i],
format(int(bars[0][i]), ','),
ha = 'center',
size = 'x-small',
weight = 'bold',
c = '#481567')
#Title.
ax.set_title("Simulation of 100,000,000 Circle Eliminations", fontsize = 24)
#Axes.
ax.set_xlabel("Last Player", fontsize = 18)
plt.xlim(0.5, 20.5)
ax.set_ylabel("Frequency", fontsize = 18)
ax.get_yaxis().set_major_formatter(FF(lambda x, p: format(int(x), ',')))
ax.tick_params(axis = 'both', labelsize = 16)
plt.xticks(ticks = list(steps), labels = list(steps));
fig.savefig("2023.07.21 Extra.png");