simulation_bin.py

Created by telpe51

Created on January 25, 2020

1.05 KB

Affiche le diagramme en barres d’une simulation de loi binomiale : affiche(N,n,p) avec N = nb de relances de la simulation, n= nb répétitions de l’expérience de Bernoulli et p= proba du succès. Exemple : affiche(1000,10,0.3) pour 1000 simulations de 10 répétitions d’une expérience de Bernoulli avec 0.3 comme probabilité du succès.


'''simulation loi binomiale et affichage diagramme en barres'''
from random import random
#from histo import *
################# module histo :
from kandinsky import *

def dg_barres(val,eff,larg=200):
    mx=min(val)
    Mx=max(val)
    my=min(eff)
    My=max(eff)
    e=int(larg/len(val)-2)
    list_x=[]
    list_y=[]
    col=color(0,0,255)
    for i in range(len(val)):
        list_x.append(int((val[i]-mx)*larg/(Mx-mx)))
        list_y.append(int((eff[i])*200/(My)))
        fill_rect(int((280-larg)/2)+list_x[i],210-list_y[i],e,list_y[i],col)
    return list_x, list_y
################ fin module histo

def bern(p):
    r=0
    if random()<p:
        r=1
    return r

def nb_suc(n,p):
    s=0
    for i in range(n):
        s=s+bern(p)
    return s

def simul_bin(N,n,p):
    list_suc=[0 for i in range(n+1)]
    for j in range(N):
        list_suc[nb_suc(n,p)]+=1
    return list_suc

def affiche(N,n,p):
    val=[i for i in range(n)]
    eff=simul_bin(N,n,p)
    dg_barres(val,eff)
    return eff
    
  ## Exemple :
#affiche(1000,10,0.3)

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.