#!/usr/bin/env python3 import random, math inside = 0 total = 10000000 for i in range(0, total): x = random.random() y = random.random() z = random.random() if math.sqrt(x**2 + y**2 + z**2) < 1.0: # will also work: # if x**2 + y**2 + z**2 < 1.0: inside += 1 area = float(inside) / total*(2**3) print("volume of cube", 2**3) print("volume sphere", area)