Advertisement
paulogp

Metodo de Monte Carlo (Pi)

Aug 7th, 2011
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # paulogp
  3. import random
  4. import math
  5.  
  6. def pi_mc(n=10000):
  7.     '''
  8.    Valor de Pi usando o Metodo de Monte Carlo
  9.    n: numero de "sementes"
  10.    '''
  11.     count_inside = 0
  12.  
  13.     for count in range(0, n):
  14.         #math.hypot: distancia Euclidiana, sqrt(x*x + y*y)
  15.         d = math.hypot(random.random(), random.random())
  16.         if d < 1: count_inside += 1
  17.  
  18.     count += 1
  19.  
  20.     print 4.0 * count_inside / count
  21.  
  22. pi_mc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement