@ Matt van Eerde
I've never seen that one before. That's a rather wonderful little problem actually.
@alexmac:
The reason it works is because you are generating a point inside a unit square, and checking whether it lies within a half-unit circle.
.-----..-----.
| .' '. |
| . '|
| <.....|
| . 0.5 . |
| . . ' |
.-------------.
<---- 1.0 ---->
The probability that the dart lands inside the circle is P(lands in circle) = P(lands in square) * (ratio of areas, square to circle). The ratio of areas is (AREA(circle(r=0.5)) / AREA(square), and the area of the square is 1x1 = 1 square unit. By construction the probability that the dart lands inside the square is 1, and therefore P(lands in circle) = Area(circle with radius 0.5) / 1 square unit
= PI * r *r / 1
= PI * (0.5 * 0.5) = PI / 4
Therefore if I throw n darts at the dartboard, I should expect (n * PI / 4) to land in the circle. In your code, you compute hits, which is the number of darts that land in the circle and throws which is the total number of darts (n), so for large number of throws, ((4 * hits) / throws) is PI, (or more strictly, as the number of throws becomes very large ((4 * hits) / throws) becomes very simmilar to PI)