# Use a montecarlo method to compute pi and then generate a PNG image with the result of the simulation # Coded by WarGame - http://vx.netlux.org/wargamevx import random import Image,ImageDraw import math im = Image.new("RGB", (100,110), "#FFFFFF") draw = ImageDraw.Draw(im) successi = 0 num = input("How many points?") for prove in range(0,num): x = random.uniform(0,100) y = random.uniform(0,100) if (((x-50)*(x-50))+((y-50)*(y-50))) <= 50*50: successi = successi + 1 draw.point([x,y],fill="red") # points in the circle else: draw.point([x,y],fill="blue") # outside the circle pi = (float(successi)/float(prove))*float(4) draw.text([50,100],str(pi),fill="green") str_file = "pi"+str(num)+".png" print 'image saved in %s' % str_file del draw im.save(str_file, "PNG")