bertrand.py

Created by ph-moutou

Created on May 08, 2018

992 Bytes

Paradoxe de Bertrand: tirage de 2 points au hasard sur un cercle trigonométrique et détermination de la probabilité (expérimentale) que la longueur soit supérieure au côté d’un triangle équilatéral inscrit dans le cercle. La méthode 1 : utilise l’équation du cercle (x tiré dans [-1;1[) et la méthode 2 : utilise l’abscisse angulaire des points (alpha tiré dans [0;2π[) ne donnent pas le même résultat!


from math import *
from random import *
def bertrand1(N):
  Total,Proba,Cote=0,0,sqrt(3)
  for i in range(N):
    Long,coord=0,[[0,0],[0,0]]
    for j in range(2):
      coord[j][0]=2*random()-1#abscisse dans [-1,1[
      coord[j][1]=(-1)**randint(0,1)*sqrt(1-coord[j][0]**2)#ordonnees
    Long=sqrt((coord[0][0]-coord[1][0])**2+(coord[0][1]-coord[1][1])**2)
    if Long>Cote:
      Proba+=1
    Total+=Long
  print("longueur moyenne=",Total/N)
  print("proba(long>cote)=",Proba/N)
  print("cote du triangle=",Cote)
  
def bertrand2(N):
  Total,Proba,Cote=0,0,sqrt(3)
  for i in range(N):
    Long,coord=0,[[0,0],[0,0]]
    for j in range(2):
      alpha=2*pi*random()
      coord[j][0]=cos(alpha)#abscisse 
      coord[j][1]=sin(alpha)#ordonnees
    Long=sqrt((coord[0][0]-coord[1][0])**2+(coord[0][1]-coord[1][1])**2)
    if Long>Cote:
      Proba+=1
    Total+=Long
  print("longueur moyenne=",Total/N)
  print("proba(long>cote)=",Proba/N)
  print("cote du triangle=",Cote)

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.