Primarily. Your code is very confusing. It is better if you call variables by name, which will give you an idea of ββwhat they mean.
I simplified the code to the desired lines. Therefore, I use a random library to create a point position.
The problem with this method is that it converges very slowly. With 10 ** 8 points I got 3.14167604.
import random
def approx_pi(points):
"""
Approximates Pi by putting dots in a square and counting
the dots in the max possible circle in that square.
:param points: Number of dots
:return: Approximation of Pi
"""
in_circle = 0
for dummy_i in xrange(points):
x_dot_position = random.random()
y_dot_position = random.random()
if x_dot_position ** 2 + y_dot_position ** 2 <= 1:
in_circle += 1
return 4.0*in_circle/points
random.random(). random.random_integers, [, ] .
, 10 ** 10 :
PI = 3,14157765
Tiempo de calculo = 3790
import multiprocessing
import time
import numpy as np
starting_point = time.time()
def approx_pi(point):
"""
Approximates Pi by putting dots in a square and counting
the dots in the max possible circle in that square.
:param points: Number of dots
:return: Approximation of Pi
"""
x_dot_position = float(np.random.random_integers(0,10**10))/10**10
y_dot_position = float(np.random.random_integers(0,10**10))/10**10
if x_dot_position ** 2 + y_dot_position ** 2 <= 1:
return 1
else:
return 0
total_points = 1*10**10
paso = 1*10**8
in_circle = 0
for in_este_bucle in xrange(0, total_points, paso):
print "Procesadores disponibles: " + str(multiprocessing.cpu_count())
pool = multiprocessing.Pool()
resultado = pool.map(approx_pi, xrange(in_este_bucle, in_este_bucle + paso))
pool.close()
pool.join()
in_circle += sum(resultado)
del resultado
print 'Total time: ' + str(time.time()-starting_point) +' seconds'
print
print 4.0*in_circle/total_points