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") point=[] def symmetry(point,swt): [a,b]=point if swt=='x': return [a,-b] elif swt=='y': return [-a,b] elif swt=='yx': return [b,a] elif swt=='y-x': return [-b,-a] else: return [a,b] fig = plt.figure(figsize = (5, 5)) ax = fig.add_subplot(111) title1 = "Tokyo University 2024 Math qu.3 NO2" random.seed(42) a1=[2,1] tdice=400000 a1cnt=0 ncnt=0 probability=[] descartes(ax, [-1, tdice/10], [0.2, 0.3],title1) an=a1 a1cnt=0 for i in range(1,tdice+1): for j in range(2): dice=random.randint(1,6) if dice==1 or dice==2: swt='x' elif dice==3 or dice==4: swt='y' elif dice==5: swt='yx' else: swt='y-x' an=symmetry(an,swt) if an==a1: a1cnt +=1 if i%10 == 0: pp=a1cnt/i probability.append(pp) yy=probability xx = np.arange(len(yy)) ax.plot(xx ,yy , color = "blue") y1=[1/4]*len(xx) ax.plot(xx ,y1 , color = "red") plt.show()