1. Brief Introduction

A classic written examination question: a wooden rod is divided into three sections, which can form the probability of a triangle and require as many answers as possible.

This article summarizes my understanding of this question, a mathematical solution, two programming solutions, one of which is a negative example.

2. Mathematical Solutions

Set the wood rod length to L and the three sections to x, y, and L-x-y respectively, where 0 <x <L, 0 <y <L, 0 <x + y <L.

To make sure that a triangle can be formed after the third section, the triangle determination theorem must be satisfied, that is, the sum of the two sides is greater than the third side, that is

X + y> L-x-y, y + L-x-y> x, x + L-x-y> y, Which is simplified to x + y> L, x <L/2, y <L/2

P (three-segment triangle probability can be formed)

= (X + y> L/2, x <L/2, y <L/2, 0 <x <L, 0 <y <L, 0 <x + y <L) area of/(0 <x <L, 0 <y <L, 0 <x + y <L)

= (0 <x <L/2, 0 <y <L/2, 0 <x + y <L/2) area/(0 <x <L, 0 <y <L, 0 <x + y <L) Area

= (1/8 * l)/(1/2 * l)

= 1/4

3. Correct Programming

Simulate the above mathematical solution through programming.

Loop and record two numbers. One is the number of times that the random length is valid, and the other is the number of times that the random length can not only be legal but also form a triangle. The random function is used as follows: x = rand () % L, y = rand () % L.

This method is equivalent to random points in the range of (0 <x <L, 0 <Y <L). For points in the lower triangle range (0 <x <L, 0 <Y <L, X + Y <L.

The calculated value is about 0.25. When the loop is the square of L, it is generally relatively stable.

4. Error Programming

In the previous programming method, two random functions are used respectively, so we need to determine whether X + Y has exceeded L. In this method, the length of X is randomly calculated, random Length of Y according to the length of X, which is equivalent to the first section of X, and then the Y and Z. In this way, the length range is controlled during each fold, so that each cycle ensures that it is legal to fold out three sections, and the loop can be used at each time.

Specific operation: the first random x = rand () % (L-2) + 1, the range of X is [1, L-2], the second random y = rand () % (L-x-1) + 1, the range of Y is [1, L-1-x], it can be seen that the range of X + Y is [2, L-1], the range of the third segment is [min {l-x-y, 1}, max {l-x-y, L-2}], so that the length must be legal.

The calculated value is about 0.19. When the LOOP is the square of L, it is generally relatively stable.

First, the correct result should be 0.25, which should be emphasized first. This method is confusing because the result of the first discount is taken into account, which seems to be consistent with the actual operation, why is the result incorrect?

In fact, the key lies in y = rand () % (L-x-1) + 1, when the folded x = L-2, then the probability of y = 1 is 100%, which is equivalent to the larger x, the smaller the value range that can be obtained by y, because every time x is random, the probability of x itself is the same, but for different x, the probability of y values is different, reflected in programming, that is

The frequencies of different x are basically the same, but for different x, the frequencies of corresponding y are different, which is equivalent to the previous area. When we add a layer of density, it is the ratio of quality, not the ratio of area. In fact, the question means that the number of all cases is the same.

This random programming makes, when x = 1, the probability of each value of y is (L-2)/L, x = L-3, each value of y is (2) /L, the frequency must be different. In some cases, the number of times is higher, and in some cases, the result is incorrect.

5. Code

The code for two programming methods is provided here.

# Include <iostream>

# Include <cstdlib>

Using namespace std;

Int L = 1000; // wood Rod Length

Int LOOP = 1000000; // number of cycles

Void method1 (){

Int x, y, z;

Int triangle = 0;

Int TMP = loop;

While (TMP --> 0 ){

X = rand () % (L-2) + 1; // [1, L-2]

Y = rand () % (L-x-1) + 1; // [1, L-x-1]

Z = l-x-y;

If (x + y> Z & x + z> Y & Y + z> X)

++ Triangle;

}

Cout <(double) Triangle/(double) loop) * 100 <"%" <Endl; // 19.3865%

}

Void method2 (){

Int x, y, z;

Int triangle = 0;

Int all = 0;

Int tmp = LOOP;

While (tmp --> 0 ){

X = rand () % L;

Y = rand () % (L );

Z = L-x-y;

If (x> 0 & x <L & y> 0 & y <L & z> 0 & z <L ){

++ All;

If (x + y> z & x + z> y & y + z> x)

++ Triangle;

}

}

Cout <(double) triangle/(double) all) * 100 <"%" <endl; // 24.9873

}

Int main (){

Method1 ();

Method2 ();

System ("PAUSE ");

Return 0;

}