A problem I solved yesterday during my 10-minute subway ride.

Given a line segment of unit length, we put two points into it randomly so that it is divided into three segments. What is the probability that these three segments can form a triangle?

Let the two points be and , and assume them to be uniformly distributed on [0:1]. If the three segments can form a triangle, it must satisfy the triangle inequality, i.e. no one side is longer than 0.5. Assume , then we must have and . The probability of such is

Similarly for the case , we must have and , and the probability is also . So the total probability is .

To verify the result, below is the Python code:

import random

def triangle():
	(x,y) = (random.random(), random.random())
	return 1 if max(min(x,y), abs(x-y), max(x,y)) < 0.5 else 0

def main():
	N = 1000000
	print "%f\n" % (sum(triangle() for i in xrange(N))/float(N))

if __name__ == '__main__':

It prints 0.25