[1] What is the mean distance between two random points on the perimeter of a unit square?
[2] What is the mean distance between two random points on the interior of a unit square?
Part 1:
DEFDBL A-Z
RANDOMIZE TIMER
FOR tr = 1 TO 1000000
p1 = RND(1) * 4
p2 = RND(1) * 4
SELECT CASE p1
CASE IS < 1
y1 = 0: x1 = p1
CASE 1 TO 2
x1 = 1: y1 = p1 - 1
CASE 2 TO 3
y1 = 1: x1 = 1 - (p1 - 2)
CASE IS > 3
x1 = 0: y1 = 1 - (p1 - 3)
END SELECT
SELECT CASE p2
CASE IS < 1
y2 = 0: x2 = p2
CASE 1 TO 2
x2 = 1: y2 = p2 - 1
CASE 2 TO 3
y2 = 1: x2 = 1 - (p2 - 2)
CASE IS > 3
x2 = 0: y2 = 1 - (p2 - 3)
END SELECT
dist = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
tdist = tdist + dist
PRINT tdist / tr
NEXT
finds an average of about 0.735 or 0.736.
Part 2:
DEFDBL A-Z
RANDOMIZE TIMER
FOR tr = 1 TO 1000000
x1 = RND(1): y1 = RND(1)
x2 = RND(1): y2 = RND(1)
dist = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
tdist = tdist + dist
PRINT tdist / tr
NEXT
finds an average of about 0.521 or 0.522
|
Posted by Charlie
on 2013-06-01 11:36:38 |