Friday, April 26, 2013

Buffon's needle simulation

Hello readers!
Have you ever heard about the Buffon's needle?
This is a very interesting experiment. In a nutshell, it is a fun approximative method to calculate the value of π. 

Watch this video to understand what is going on:



After playing for 20 minutes I came with a simulation in python and I thought I could share it with you. Please, be nice. It was a quick hacking, ;)

The code:

import sys
import math
import random


def get_random(l, k):
    return random.random()*(l - 2*k) + k

def get_point(w, h, k):
    return (get_random(w, k), get_random(h, k))

def get_angle():
    return random.random()*2*math.pi

def intercept(p1, p2, h, k):
    for line in range(0, h+1, k):
        if (line >= p1[1] and line <= p2[1]) or \
           (line <= p1[1] and line >= p2[1]):
            return True

def drop_and_check(w, h, k):
    p1 = get_point(w, h, k)
    angle = get_angle()
    p2 = (p1[0] + (k/2.0)*math.cos(angle), 
          p1[1] + (k/2.0)*math.sin(angle))
    return intercept(p1, p2, h, k)

def repeat(times):
    w = h = 1000
    k = 10
    count = 0
    for i in range(times):
        if drop_and_check(w, h, k): count += 1
    return float(times)/count

if __name__ == "__main__":
    print(repeat(int(sys.argv[1])))

It is available via github too.

Hope you liked it! =)

Till the next post!
Ronald Kaiser

No comments:

Post a Comment