galton_board.py

Created by alain-busser

Created on June 30, 2019

725 Bytes

Simulation de la planche de Galton (programme de terminale à partir de la rentrée 2020). board(N) simule le trajet de N billes. On suppose que 0<N<159 et plus il y a de billes à lancer, plus ça va vite.


from turtle import *
from kandinsky import *
from random import *
effectifs=[0]*21
def initialisation():
  penup()
  for y in range(10):
    for x in range(-y,y+1):
      if (x+y)%2==0:
        fill_rect(159+16*x,16*y-1,3,3,color(64,24,8))

def une_bille(hauteur):
  goto(0,100)
  for k in range(20):
    setheading(randrange(-135,-43,90))
    forward(8*2**0.5)
  setheading(-90)
  x = int(round(position()[0],0))
  effectifs[x//16+10] += 1
  y = 220-hauteur//2*effectifs[x//16+10]
  fill_rect(150+x,y,20,hauteur,color(200,150,50))

def board(N):
  assert N==int(N) and 0<N<160
  global effectifs
  effectifs = [0]*21
  initialisation()
  speed(N//20+1)
  for _ in range(N):
    une_bille(int(160//N)+1)

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.