import random
import numpy as np
import matplotlib.pyplot as plt

def descartes(ax, ran_x, ran_y, ax_title,x_label = "x", y_label = "y"):
    ax.set_xlabel(x_label, fontsize = 12)
    ax.set_ylabel(y_label, fontsize = 12)
    ax.set_xlim(ran_x[0], ran_x[1])
    ax.set_ylim(ran_y[0], ran_y[1])
    ax.set_title(ax_title, fontsize = 14)
    ax.grid()
    ax.axhline(0, color = "black")
    ax.axvline(0, color = "black")


fig = plt.figure(figsize = (5, 5))
ax = fig.add_subplot(111)

title1 = "Tokyo University 2023 Math qu.2 NO2"
descartes(ax, [-1, 500], [101, 105],title1)

random.seed(42)
roll_ball_n = 660
total_n = 10
loop_n=501

ball_box = [10,10,10,1,1,1,1,100,100,100,100,100]

Flag1=False
cnt1=0
y=[]
yy=[]



for ll in range(loop_n):
    for k in range(total_n):
        for i in range(1,roll_ball_n+1):
            random.shuffle(ball_box)
            for j in range(11):
                Flag1=True
                if ball_box[j]+ball_box[j+1] == 2 or ball_box[j]+ball_box[j+1] == 20:
                    Flag1=False
                    break
            if Flag1==True:
                cnt1 += 1
        y.append(cnt1)
        cnt1=0
    yy.append(np.mean(y))

print("yy average=",np.mean(yy))

xx = np.arange(len(yy))

ax.plot(xx ,yy , color = "blue")

xx = np.arange(len(yy))
b = [103]*len(yy)

ax.plot(xx ,b  , color ="red")

plt.show()