Average Distance

Two points are chosen randomly inside a square with side length 1. What is the average distance between the two points?

Round your answer to 3 decimal places.


The answer is 0.521.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Steven Chase
Jul 16, 2016

Here's my Python code. It runs 10 times to check for consistent results. Average distance is 0.521.

@Steven Chase OMG this was the same question which I also posted 1 month ago sir.
I didn't know that this question is already posted by someone 4 years ago.

Talulah Riley - 10 months ago
Snehal Shekatkar
Jul 16, 2016

Since the problem is in computer science category, let me provide the "computer solution". Below is the simple Python snippet which gives the required value. It is helpful to run this several times just to make sure that the answer doesn't change up to the third decimal. I got 0.521 0.521

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# import two modules that we need
import random
import math

n = 10000000 # This is the total number of data points. Take this as large as possible to get accurate enough answer
ave = 0 # This is the average distance

for i in range(n):
    # Generate two random points inside the square of side length 1
    p1 = (random.random(), random.random())
    p2 = (random.random(), random.random())

    ave += math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)


ave = ave/n

# Print the value of the average
print(ave)

Aaron Tsai
Jul 5, 2016

We can place the square in the coordinate plane. From there we can use the distance formula and set the x x and y y values to random values. Then we set up a loop to compute the distance a certain number of times, and store those values to a list, in which we take the mean of. Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#import the random and math modules 
import random
import math

a = []

#set up loop for 10000000 trials
for i in range(0,10000000):

    #Distance formula
    x = math.sqrt(((random.random()-random.random())**2)+((random.random()-random.random())**2)) 

    #stores value to list
    a.append(x)

#prints average
print sum(a) / float(len(a)) 

This returns approximately 0.521 \boxed{0.521} . You can run the code here .

Could you add details so that people can look at your solution and understand how to approach​ it? Thanks!

Calvin Lin Staff - 4 years, 11 months ago

Well, this isn't my solution; the way I solved it was with a computer program. I didn't understand all of the solution in the video...

So I can't provide a more detailed solution than the video... sorry about that.

Aaron Tsai - 4 years, 11 months ago

Log in to reply

Can you add your CS approach to the solution? Thanks!

Calvin Lin Staff - 4 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...