import random import matplotlib.pyplot as plt
# importing modules that we will need
import random
import matplotlib.pyplot as plt
# initializing the number of "throws"
num_points = 1000
# here we "throw darts" and count the number of hits
points = []
hits = 0
for _ in range(num_points):
x, y = random.random(), random.random()
if x*x + y*y < 1.0:
hits += 1
points.append((x, y, "red"))
else:
points.append((x, y, "blue"))
# unzip points into 3 lists
x, y, colors = zip(*points)
# define figure dimensions
fig, ax = plt.subplots()
fig.set_size_inches(6.0, 6.0)
# plot results
ax.scatter(x, y, c=colors)
<matplotlib.collections.PathCollection at 0x111e21f90>
# compute and print the estimate
fraction = hits / num_points
4 * fraction
3.164