galton.py

Created by niz-moussatat

Created on May 15, 2018

599 Bytes

Une simulation d’une planche de Galton pour illustrer le théorème de Moivre-Laplace


from math import * 
from kandinsky import * 
import random

N=5000 # nombre de billes lachees 
L=150 # nombre de lignes de la planche

List=[0,]*(2*L)

for n in range(N):
  pos=L # initialement, la bille est centree
  for l in range(L):
    r=random.random()
    if r>0.5:
      pos=pos+1 # la bille va a droite
    else: 
      pos=pos-1 # la bille va a droite
  List[pos]=List[pos]+1

# plotlist(List)

red=color(255,0,0)
Ymin=min(List)
Ymax=max(List)
length=len(List)
for i in range(length):
  j=int((List[i]-Ymax)*182/(Ymin-Ymax)+19)
  for k in range(j,200):
    set_pixel(i,k,red)

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 cookies policy.