Okay got it.
]]>Perhaps the R code would be more readable:
r=sqrt(runif(10000000,0,1))
th=2*pi*runif(10000000,0,1)
mean(sqrt(1-(r*sin(th))^2)-r*cos(th)) # 0.8485681
mean(sqrt(((r-cos(th))^2)+(sin(th))^2)) # 1.131671
Could you pseudocode that?
]]>A point 'P' inside a unit circle is chosen at random.
a) A direction is chosen at random at P and a line is drawn from P to the point in the circumference 'Q' in that direction. What is the expected length of the line segment PQ ?
b) A direction is chosen at random at the center and a line is drawn from P to the point in the circumference 'Q' in that direction. What is the expected length of the line segment PQ ?
Analytical answer is given in #147.
Here, we'll see how to do a simulation in J.
Suppose 'r' is the distance between P and center of the circle, and θ is the angle chosen,
we can write the line segment length as
load 'trig'
r=:%:?10000000#0
th=:2p1*?10000000#0
(+/%#)(%:(1-(r*sin th)^2))-r*cos th NB. appx. 0.848544
(+/%#) %:(((r-cos th)^2)+(sin th)^2) NB. appx. 1.131613