sim_echant.py

Created by gilles-dhaussy

Created on June 16, 2024

723 Bytes


from math import sqrt
from random import random
from matplotlib.pyplot import *

def succes(p):
  if random()<p:
    res=True
  else:
    res=False
  return res

def frequence_succes(n,p):
  nb_succes=0
  for i in range(n):
    if succes(p):
      nb_succes+=1
  f_succes=nb_succes/n
  return f_succes

def graph_sim(N,n,p):
  axis((-0.1*N,1.1*N,-0.1,1.1))
  text(0,1.1,"N={} et n={}".format(N,n))
  plot([-0.1*N,1.1*N],[p,p],
    color="red")
  plot([-0.1*N,1.1*N],
    [p-1/sqrt(n),p-1/sqrt(n)],
    color="green")
  plot([-0.1*N,1.1*N],
    [p+1/sqrt(n),p+1/sqrt(n)],
    color="green")
  Lx=list(range(1,N+1))
  Ly=[]
  for i in range(N):
    Ly.append(frequence_succes(n,p))
  scatter(Lx,Ly)
  show()

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.