Thanks for the snippet. You can control N by generating a large enough collection by increasing N_rand – but it’s not deterministic hope my tries. Just keep adding more attempts until you reach N suitable ones.

–loren

]]>N_rand = 1000; x_rand = rand(1, N_rand) - 0.5; y_rand = rand(1, N_rand) - 0.5; r_rand = sqrt(x_rand.^2 + y_rand.^2); ix = r_rand <= 0.5; cos_rand = x_rand(ix)./r_rand(ix); sin_rand = y_rand(ix)./r_rand(ix); N = sum(ix);

The only problem that you will not control N.

Daniel

]]>OK, thanks for the update!

]]>This is the same concept, simplified to some extreme. It is equivalent to generating needles of length 2 by selecting a center point in the range x=[-1,1], where it could intersect the line at x=0. Note that the y-axis location is irrelevant!

x = rand(1,N) * 2 - 1; angs = rand(1,N)*360; n = sum(abs(x) < abs(cosd(angs))); results(ii,2) = 2 * N / n;]]>

–loren

]]>Though I think it’s silly to use cos and sin to get an estimate of pi… :)

]]>