Reputation: 2896
As per request, pseudocode (I am not a coder, so not good pseudocode here), but,
Finally, pi_est <- (inside/(inside + outside))*4
Upvotes: 0
Views: 359
Reputation: 8252
As I suggested might be the issue in comments -- with the random walk scheme you're using, I believe you're not going to visit each square equally often... and that is going to make your estimate inaccurate (biased).
I suggest you set up a small grid (say 5x5) and run it a long time, then I'd suggest accumulating the results for the six different cell types:
C E M E C (Corner, Edge, Midedge,
E D O D E Diagonal, Orthogonal,
M O X O M Xenter)
E D O D E
C E M E C
(Since - by symmetry - any differences within those types should be noise-only).
Then (scaling for the fact that the respective counts of each type of cell are 4,8,4,4,4,1) you should be able to see whether they're visited in the right proportions (16%,32% and 4% for 4-, 8- or 1- counts respectively). I believe what you'll find is that the boundary cells are visited more often (relative to their fair proportion) than the middle.
One suggestion would be to "wrap" the edges to that the top and bottom edges and the left and right edges communicate. This avoids the "bounceback" at the edge and instead makes every cell like every other cell; by symmetry you should get equal probability for each cell.
Upvotes: 2