Cut one stick twice to turn it to be three sticks, what is the probability that the three sticks form a triangle? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionWell it is not a program problem. Is there any hint for such quiz? I am thinking about focusing on two random R1, 开发者_开发技巧R2, both of which is in range (0, 1). and supposing R2 > R1 and then fulfill two equation:
R1 + (1 - R2) > R2 - R1 // two sticks sum longer then the rest one
|R1 - (1 - R2)| < R2 - R1 // the difference of these two should be shorter the rest one
but I cannot move further...
Think of (r1, r2) as a point in the unit square.
Which part of the unit square is allowed for r2 > r1?
Which part of that leads to three lengths that can form a triangle?
The answer is 1/4. Here is the explanation.
Let x is the length of the leftmost stick and y is the length of the rightmost stick. Then the middle stick has length n-x-y, if the original stick's length was n.
The possible values for x,y are those for which:
- x > 0
- y > 0
- x + y < n
In the plane Oxy this is equivalent to say that the point (x,y) lies within the triangle with vertices (0, 0), (n, 0), (0, n).
Now these three numbers (x, y, n-x-y) form a triangle if all of the three are satisfied:
- x + y > n - x - y <=> x + y < n/2
- x + (n - x - y) > y <=> y < n/2
- y + (n - x - y) > x <=> x < n/2
Again in the Oxy plane these are satisfied when the point (x,y) lies within the triangle with vertices (0, n/2), (n/2, n/2), (n/2, 0).
The area of this triangle is a quarter of the area of the (0, 0), (n, 0), (0, n) triangle, since it's the 'middle' triangle (whose vertices are the midpoints) of the bigger one.
Here is a simple C# program to verify the answer:
Random r = new Random();
int count = 0, total = 0, tries = 1000000;
double x, y;
for (int i = 0; i < tries; i++)
{
x = r.NextDouble();
y = r.NextDouble();
if (x + y > 0.5 && x < 0.5 && y < 0.5) ++count;
if (x + y < 1.0) ++total;
}
Console.WriteLine((double)count / total);
I have just made a program to verify it, and I found it is 1/4:
class Program
{
static void Main(string[] args)
{
int nIsTriangle = 0;
Random ran = new Random(0);
int nTry = 1000000;
for (int i = 0; i < nTry; i++)
{
double r1 = ran.NextDouble();
double r2 = ran.NextDouble();
if (Check(r1, r2)) nIsTriangle++;
}
Console.WriteLine((double)nIsTriangle / (double)nTry);
Console.ReadKey();
}
static bool Check(double r1, double r2)
{
double first = Math.Min(r1, r2);
double second = Math.Abs(r1 - r2);
double third = 1 - Math.Max(r1, r2);
bool conditionA = (first + second) > third;
bool conditionB = Math.Abs(first - second) < third;
return conditionA && conditionB;
}
}
精彩评论