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)