alea.py

Created by redgl0w

Created on July 13, 2022

765 Bytes


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

def tirage():
  alea=random()
  if alea<=0.5625:
    return -40
  elif alea<=0.9375:
     return 30
  else:
    return 100

result={}
taille=5000
for i in range(taille):
  t=tirage()
  if t in result.keys():
    result[t]+=1
  else:
    result[t]=1

for i,j in result.items():
  bar(i,j)

x=(abs(max(result.keys()))-abs(min(result.keys())))/3
y=max(result.values())
window=(abs(max(result.values()))-abs(min(result.values())))/10

E=0
for i,j in result.items():
  E+=i*(j/taille)
text(x,y,"E(X)={}".format(E))

V=0
for i,j in result.items():
  V+=(i-E)**2*(j/taille)
text(x,y-window,"V(X)={}".format(V))

sigma=sqrt(V)
text(x,y-window*2,"sigma(X)={}".format(sigma))

grid()
show()