The code below finds the approximation value of a PI through parallelising and filtering. The approach requires me to pass itr as an argument inside the calc_points_in_circ(itr) to work. The issue is, the variable itr value is unused and has no intentions to be part of the calculation logic. How can this be rewritten without any unused variables, and still make use of PySpark parallelisation?
from pyspark.context import SparkContext
from pyspark.sql.session import SparkSession
import random
sc = SparkContext("local", "PI Approx")
spark = SparkSession(sc)
pointsInCircle = 0
n = 1_000_000
def calc_points_in_circ(itr):
x = random.random()
y = random.random()
# Pythagorean theorem; c = math.sqrt(a^2 + b^2)
distance = (x*x + y*y) ** 0.5
return distance < 1
rdd = sc.parallelize(range(0, n))
pointsInCircle = rdd.filter(calc_points_in_circ).count()
pi = 4 * pointsInCircle / n
print(f"PI approximation: {pi:.3f}")