Skip to content

Commit

Permalink
FIX: subject Royaume de Monte-Carlo
Browse files Browse the repository at this point in the history
  • Loading branch information
VecoMr committed Oct 7, 2023
1 parent ebbad92 commit bd0ee2f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Royaume_de_Monte-Carlo/Cobra/monte-carlo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from src.show import *

def main():
NB_ESSAI = 10_000

def simulation(nb_essai):
nb_point_total = 0
nb_point_in_circle = 0

for _ in range(nb_essai):
# On choisi au hasard notre abscisse entre 0 et 1
x = my_random(0, 1)
# On choisi au hasard notre ordonnée entre 0 et 1
y = my_random(0, 1)

nb_point_total += 1

if x**2 + y**2 < 1:
nb_point_in_circle += 1

pi = nb_point_in_circle / nb_point_total * 4

# Afficher l'approximation de PI
print(pi)
display_round(mode="animate")
simulation(NB_ESSAI)


if __name__ == "__main__":
main()
Binary file not shown.
File renamed without changes.
File renamed without changes.
131 changes: 131 additions & 0 deletions Royaume_de_Monte-Carlo/Participant/src/show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import random

from matplotlib import pyplot as plt
from matplotlib import animation


class Viewer():
def __init__(self) -> None:
self.x = []
self.y = []
pass

def append(self, el):
if (len(self.x) + len(self.y)) % 2 == 0:
self.x.append(el)
else:
self.y.append(el)

def __sep_point(self):
li = [[], [], [], []]
for i, j in zip(self.x, self.y):
if i**2 + j**2 < 1:
li[0].append(i)
li[1].append(j)
else:
li[2].append(i)
li[3].append(j)
return li

def show(self):
print(len(self.x), len(self.y))
if len(self.x) != len(self.y):
raise Exception("Show : Bad array len")
fig, ax = plt.subplots()
circle = plt.Circle((0, 0), 1, fill=False,
edgecolor="red", linewidth=3)
ax.add_artist(circle)
a, b, c, d = self.__sep_point()
ax.plot(a, b, 'ro')
ax.plot(c, d, 'bo')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

def __animate_step(self, frame):
i, j = (self.x_cp.pop(0), self.y_cp.pop(0))
if i**2 + j**2 < 1:
self.a.append(i)
self.b.append(j)
else:
self.c.append(i)
self.d.append(j)
im = []
im += self.ax.plot(self.a, self.b, 'ro')
im += self.ax.plot(self.c, self.d, 'bo')
return im


def animate(self, speed):
self.x_cp = self.x[:]
self.y_cp = self.y[:]
self.a = []
self.b = []
self.c = []
self.d = []
fig, self.ax = plt.subplots()
circle = plt.Circle((0, 0), 1, fill=False,
edgecolor="red", linewidth=3)
self.ax.add_artist(circle)
self.ax.set_xlim(0, 1)
self.ax.set_ylim(0, 1)
ani = animation.FuncAnimation(fig, self.__animate_step, frames=range(5), interval=100/speed, blit=True)
plt.show()


win = Viewer()


def my_random(a: int, b: int) -> float:
"""
Generate a random float number between a and b.
Parameters
----------
a : int
The minimum value of the random number range.
b : int
The maximum value of the random number range.
Returns
-------
float
A random float number between a and b.
"""
rng = random.uniform(a, b)
win.append(rng)
return rng



def display_round(view: Viewer = win, speed: int = 1, mode: str = "animate") -> None:
"""
Display the round using the specified mode.
Parameters
----------
speed : int, optional
The speed of the animation, by default 1.
mode : str, optional
The mode to use to display the round, either "animate" or "show", by default "animate".
Raises
------
Exception
If an invalid mode is specified.
Returns
-------
None
"""
if mode == "animate":
view.animate(speed)
elif mode == "show":
view.show()
else:
raise Exception("display_round : Bad mode")


if __name__ == "__main__":
with open("help.txt", "r") as f:
print(f.read())
File renamed without changes.

0 comments on commit bd0ee2f

Please sign in to comment.