This is an old revision of the document!
flip.py
import random total = 0 for i in range(2000000000000000000): flip = random.randint(0, 1) total = total + flip average = total / (i + 1) print(i, flip, total, average)
pi.py
import random def main(): N_sq = 0 N_circ = 0 n_iterations = 10000000000 for i in range (n_iterations): x = random.random() * 2 - 1 y = random.random() * 2 - 1 N_sq = N_sq + 1 #Always in square if x*x + y*y < 1: N_circ = N_circ + 1 pi_estimate = 4.0 * float(N_circ) / float(N_sq) if i % (n_iterations/10000000000) == 0: print('%d %f' % (i, pi_estimate)) main()