Méthode de Monte Carlo

Début :

Fin :



from random import* def f(x): return x*x def montecarlo(f,a,b,n): cpt = 0 for i in range(0,n): x = uniform(a,b) y = uniform(0, max(f(a),f(b))) if f(x) > y: cpt = cpt+1 return (1.0*cpt/n)*(b-a)*max(f(a),f(b)) print(montecarlo(f,0,2,10)) print(montecarlo(f,0,2,100)) print(montecarlo(f,0,2,1000)) print(montecarlo(f,0,2,10000))