monte_carlo.py

Created by elodie-gamot

Created on October 08, 2021

388 Bytes

Approximation de Pi par la méthode de Monte-Carlo. On s’intéresse au nombre de points à l’intérieur d’un cercle de centre (160,110) et de rayon 110. Le nombre de points dans le cercle correspond à pi r^2 tandis que le nombre de points dans le carré correspond à 4 r^2.


from math import *
from random import *
from kandinsky import *

def crible(n):
  red=color(255,0,0)
  blue=color(0,0,255)
  inside=0
  outside=0
  for i in range(n):
    x=randint(50,270)
    y=randint(0,220)
    if (x-160)**2+(y-110)**2<110**2:
      set_pixel(x,y,red)
      inside+=1
    else:
      set_pixel(x,y,blue)
      outside+=1
  #return inside/(0.25*n)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.