I was solving the following probability problem yesterday:
Given a rod of length L, find the number of ways to cut the rod into thre parts s.t. these parts form a triangle.
Now, it's a standard problem and multiple solutions can be found online. Added a few links in solutions (Feel free to go and explore). Obviously, all of them arrive at the same conclusion that the required probability is 1 / 4 = 0.25.
Then, I decided to conduct an experiment and write a python script to simulate the experiment. I perform the experiment 10^7 times (takes a while to calculate the result) but always comes up with ~0.19 no matter what I choose number of experiments and length of rod to be.
I would like to find out why theoretical and practical result doesn't match?
Here is my python script:
import numpy as np def rod_cut(L): x = np.random.uniform(0, L) y = np.random.uniform(0, L - x) z = L - (x + y) return (x, y, z) def is_triangle(datapoint): a, b, c = datapoint return a + b >= c and \ b + c >= a and \ a + c >= b def perform_experiment(): N = 10000000 L = 1.0 datapoints = [rod_cut(L) for _ in range(N)] successes = sum([is_triangle(datapoint) for datapoint in datapoints]) print("Probability of cutting rod s.t it makes a triangle = ", successes / N) perform_experiment()
Note: I tried making a frequency distribution graph of the random numbers that
numpy generates and it comes out to be uniform. So I trust that
numpy.random.uniform is generating a uniform distribution of random numbers as expected.
Solution based on algebraic inequalities:
1. (From quora) [https://www.quora.com/A-rod-is-broken-into-three-parts-what-is-the-probability-that-the-three-parts-can-be-arranged-to-form-a-triangle]