A famous problem in probability is this: Suppose you randomly1 break a stick in two places. You will get three smaller pieces of stick. What is the probability that you can form a triangle using these pieces?
One interesting thing about this problem is that depending on how you visualize breaking the stick, you get different probabilities.
In this post, we will see two such scenarios. We will also see how we can confirm our answers using Python simulations.
First Scenario
A simple way to break a stick into two is to randomly make two cuts and pull the pieces apart only after both the cuts have been made. The cuts may be like the following:

Or the cuts may be in the opposite order, where Cut 1 comes after Cut 2. Remember that this is possible because we are making both the cuts uniformly at random.
We can assume the length of the stick to be some

Or like this:

Now how do we proceed to compute the probabilities? I will give a hint: for three lengths to be the lengths of sides of a triangle, the sum of any two of the lengths should be greater than the third length. (Why this condition is necessary and sufficient? If one side is longer than the other two sides added together, think about how you would draw the triangle using the longest side as the base.)
If you want to give a shot at solving the problem by your own, now is a good time.
In the first case, the lengths of the pieces are
Computing the actual probability is quite easy at this point. Both

In the graph, region (the dashed triangle at the top left) contains the values
of
If you are convinced by the argument in the last paragraph, great! In any case, it would be great to be able to conduct some experiment to confirm our calculations. One way to do that would be to take lots of sticks, break them randomly and see whether we are able to form triangles in roughly a quarter of cases. But that is difficult — not just because of the physical exertion involved, but to actually break a stick uniformly at random is difficult for humans. We tend to break sticks more towards the middle. So we go for the better option and ask Python to do the hard work for us.
Python is great for simulating random experiments. In this case the program is
particularly simple: all we need to do is to generate two random numbers in the
range of
import random as rand
def istriangle(a, b, c):
return a + b > c and a + c > b and b + c > a
def break_one_stick():
x, y = rand.uniform(0, 1.0), rand.uniform(0, 1.0)
if x > y:
x, y = y, x
return istriangle(x, y - x, 1 - y)
def count_triangles(num_trials):
num_triangles = 0
for i in range(num_trials):
if break_one_stick():
num_triangles += 1
return num_triangles
if __name__ == '__main__':
trials = 100000
triangles = count_triangles(trials)
print("Estimated probability = {0:0.4f}".format(triangles/trials))
For me this code returns 0.2489, 0.2502 etc on different runs. This is a nice confirmation of the mathematical answer.
Second Scenario
Now consider another way to break a stick: you make one cut and take out the first (left) piece. Then you make a cut on the remaining second (right) piece. What is the probability that the three pieces you get in this manner form a triangle?
Here we know that
Therefore, for any first cut
Thus for a given
This is an easy to evaluate integral which turns out to be exactly
Anyway, let us now turn to Python for confirmation. Only one function need to change from the previous code. Here it is:
def break_one_stick():
x = rand.uniform(0, 1.0)
y = rand.uniform(x, 1.0)
return istriangle(x, y - x, 1 - y)
Yes, in fact, the function is now simpler in a sense because we no longer have
to check which of
For me, the simulation gives answers like
These two are not the only scenarios possible, by any means. One can think up lots of other ways of cutting a stick. Once the first cut is made, the bigger of the two pieces can be selected for the next cut. Or, the piece for the second cut can itself be selected based on a coin toss. I think all of these scenarios can be solved similarly. In any case simulation of all the scenarios is very easy using Python.
Footnotes
-
Uniformly at random, to be precise. ↩