A member of the EveAndersson.com community since October 11, 2013

- Personal home page: http://www.brainamp.com

- October 11, 2013
Hi Eve,

Here is another python program to calculate pi. It uses the PIL module to make a PNG showing the circle inscribed into the square.

Thanks!

#!/usr/bin/env python from random import random from math import hypot from PIL import Image from PIL import ImageDraw TRIALS=10**6 XMAX=500 YMAX=500 def main(): inside = 0 img = Image.new('RGB', (XMAX, YMAX), "white") draw = ImageDraw.Draw(img) for i in xrange(TRIALS): x = random() y = random() if hypot(x, y) < 1: inside +=1 draw.point((int(x*XMAX), int(y*YMAX)), fill="red") img.save('pi.png') print 'I think pi is: ', 4.0 * inside / TRIALS if __name__ == '__main__': main()

*Image: pi.png*

-- (October 11, 2013) on Calculation of Pi Using the Monte Carlo Method

eve@eveandersson.com