msfz751

py_calc.py

Oct 18th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. py_calc.py
  2. # Generates approximations of pi.  Put the number of trials you want in A1. More
  3. # trials will result in higher accuracy.
  4.  
  5. from random import random
  6. from time import sleep
  7.  
  8. def pi_calc(n):
  9.     while Cell("B2").value < n:
  10.         x, y = Cell("D2"), Cell("E2")
  11.         r = Cell("F2")
  12.         x.value, y.value = random(), random()
  13.         r.value = x.value**2 + y.value**2
  14.         Cell("B2").value += 1
  15.         if r.value <= 1:
  16.             Cell("B3").value += 1
  17.         Cell("B4").value = 4*float(Cell("B3").value)/Cell("B2").value
  18.         sleep(0.01)
  19.  
  20. trials = Cell("A1").value
  21. if (type(trials)!= int) or (trials < 1):
  22.     trials = 100
  23.  
  24. # sheet setup
  25. Cell("A2").value, Cell("A3").value, Cell("A4").value = "Trial:","Hits:","pi is about:"
  26. Cell("D1").value, Cell("E1").value, Cell("F1").value = "x","y","x^2 + y^2"
  27. Cell("B2").value, Cell("B3").value = 0,0
  28.  
  29. pi_calc(trials)
Advertisement
Add Comment
Please, Sign In to add comment