Probability problem (I.)

Source: Internet
Author: User

Reprint http://noalgo.info/414.html

Probability theory is one of the most important basic subjects in computer science, and the probability problem is also a frequently encountered issue in the process of job search.
Here is a summary of some of the classic probability questions as an exercise.

1. Randomly select a point in a circle with a radius of 1.

Method 1: Randomly select a point on the x-axis [ -1,1],y axis [ -1,1], which is the desired point if the point is inside a circle. If it is not inside the circle, it is re-randomly until it is selected.

Method 2: Randomly select an angle from [0, 2*pi] and then randomly select a point in the radius of this direction. However, the points on the radius can not be evenly selected, and the probability of selection is proportional to the distance from the center, in order to ensure that the random points are evenly distributed within the circle.

2. A stick, cut into three sections, the probability of forming a triangle?

Set the first section X, the second segment Y, and the third paragraph 1-x-y.

Consider all possible truncation methods. The possible truncation method must ensure that three edges are positive and less than the original edge length, then there is 0<x<1,0<y<1,0<1-x-y<1, drawing, (x, y) must be in the lower left corner of the unit square half right triangle, an area of 1/2.

Then consider the truncation method that can form a triangle. First of all to meet just the three conditions 0<x<1,0<y<1,0<1-x-y<1, and then must meet the requirements of the triangle side, that is, the sum of the two sides is greater than the third side, x+y>1-x-y,x+1-x-y>y,y+1- X-y>x, the simplification is to be

0<x<1/2,0<y<1/2,1/2<x+y<1

Drawing shows that at this point (x, y) must be in the upper-right corner of the triangle with a 1/2 edge length of half right triangle, an area of 1/8.

The final probability is (1/8)/(1/2) = 1/4.

3. Throw a six-side dice, throw them until they are 6, and ask how many times you want to throw them.

Because the probability of each throw to 6 is equal, is 1/6, so the expected number of times is 1/(1/6) = 6 times.

The following is a different solution, assuming that the expected number of times is E. Consider the first throw, if it has been thrown to 6 (probability is 1/6), then you do not have to throw. If you don't throw it at 6 (the probability is 5/6), then you still need to throw it, but how many times? Obviously, now it's starting to know that the number of throws to 6 is still e, but it's just been thrown once, so you can get this equation.

E = 1 * 1/6 + (1 + E) * 5/6,

Solution to E = 6. That is, the expected number of times is 6.

4. A wooden barrel has a m white ball, every minute from the bucket randomly take out a ball painted red (whether white or red are red) and then put back, ask the bucket of the ball all the expected time to paint red?

Make the bucket have I red ball and then put all the ball red the expected time is a[i], at this time to take out a ball, if it is red (probability is i/m), then directly put back, and the remaining expected time is still a[i]. If it is white (the probability is 1-i/m), then put back after applying red, the remaining expected time is a[i+1], then

A[i] = (1 + a[i]) * i/m + (1 + a[i+1]) * (1–i/m)

i.e. A[i] = a[i+1] + m/(m-i)

Obviously, there is a[m] = 0

can be solved a[0] = m/m + m/(M-1) + ... + m/1 + 0

5. You have a sword. With each gem used, a 50% probability of success will make the sword ascend one level, and the probability of 50% will fail. If the sword's progression is greater than or equal to 5, then failure will cause the sword to fall to 1 levels. If the sword's progression is less than 5, failure has no effect. The question is: how many gems can I expect to get a 1-level sword up to level 9?

The problem is relatively simple, using a[i] to indicate the number of gems to be used from level i-1 to Grade I.

When i<=5 is not degraded, the expected quantity is 2, i.e. a[2] = a[3] = a[4] = a[5] = 2

When I>5, because it will be degraded, the success of a gem is enough, the unsuccessful need to reverse the level, need to first use a[i-1] a gem first to return to the i-1 level, and then use A[i] a gem rose to the level I, namely

A[i] = 1 * + (1 + a[i-1] + a[i]) * 1/2

ie a[i] = a[i-1] + 2

A[6]= 4, A[7] = 6, a[8] = 8, a[9] = 10

The number of gems required for levels 1 to 9 is a[2]+...+a[9] = 36.

6. A function of Rand7 () is known to return 1 to 7 random natural numbers, how to use this Rand7 () to construct RAND10 (), Random 1~10.

The main principle of generating random numbers is that the probability of occurrence of each number is equal, and if you can get a set of equal probabilities, you can find a method that maps to 1~10.

Rand7 () returns the natural number of 1~7, constructs a new function (Rand7 ()-1) *7 + Rand7 (), which randomly generates the natural number of 1~49. The reason is that each number in 1~49 is represented by only the value of the first Rand7 () and the value of the second Rand7 (), so the probability of their occurrence is equal.

But there are too many numbers, you can discard 41~49 numbers, divide the number of 1~40 into 10 groups, each group maps to one of the 1~10, so you can get random results.

The method is to use (Rand7 ()-1) *7 + Rand7 () to produce a random number x, if greater than 40 continues to be random until less than or equal to 40, if less than or equal to 40, the resulting random number is (x-1)/4+1.

7. A function of RANDM () is known to return 1 to M random natural number, how to use this RANDM () construct Randn (), Random 1~n.

Extension of the above topic.

Can be obtained directly when n<=m.

When n>m, similar constructs (RANDM ()-1) *m + RANDM () can produce 1~m^2 (that is, randm^2), and you can select the mappings of n constructed m^2 in 1~n.

If m^2 is still not large, it can continue to be constructed for randm^2 until it succeeds.

8. It is known that a random generator, the probability of generating 0 is P, the probability of generating 1 is 1-p, now you want to construct a generator, so that it produces 0 and 1 of the probability are 1/2.

Consider a continuous generation of two random numbers, the result is only four possible: 00, 01, 10, 11, where the probability of producing 01 and generating 10 is equal, are p* (1-p), so you can use the probability of the same characteristics, such as probability to produce 01 random numbers.

For example, Map 01 to 0 and 10 to 1. So the whole solution is:

Generates two random numbers, if the result is 00 or 11 discards, if the result is 01 produces 0, the result is 10 produces 1.

9. It is known that a random generator, the resulting number distribution is not clear, now you want to construct a generator, so that it produces 0 and 1 of the probability are 1/2.

Similar thinking, consider the continuous generation of two random numbers a, B, the result has three cases a==b,a>b,a<b, in which because of the symmetry of A and B, the probability of a>b and a<b appear equal, so you can use the probability of equal probability to produce 01 random number. method is similar.

Alternatively, you can find another event of equal probability, such as selecting a threshold th, dividing the result of a random number into less than a threshold and a threshold of greater than or equal to two cases, so that two random numbers are generated consecutively, one is less than the threshold, and the other is equal. Then a random number is generated similar.

10. A random generator is known to generate 0 of the probability is P, the probability of generating 1 is 1-p, constructs a generator, so that it constructs 1, 2, 3 probability are 1/3; More generally, the construction of a generator, so that it constructs 1, 2, 3 、... n probability are 1/n.

At this point we already know that to produce a random number moderately from the n number, the key is to find N or more occurrences of equal probability, and then we randomly generate the event, if it is directly ignored by events of N events, until one of these n events is produced, The random number that matches the event is then generated. Since n events occur in equal probability, the probability of the resulting random number is also equal.

Consider the successive generation of X random number, the result should be X 0 and 1 of the combination, in order to make certain results appear equal probability, we should let this result in the same number of occurrences of 0 and 1, that is, each half. Therefore, the length of x must be even, for convenience, consider the continuous generation of 2x random numbers. Each half of the 0 and 1 results can be given a number of 1 to N, in order to be able to represent the n number, 0 and 1 each occurrence of the total result is greater than or equal to N, that is

C (2*x, x) >= n

Solving the smallest x is the most efficient x.

The first n 0 and one half of the results are then given a value of 1 to N, respectively. The number of 2*x is continuously generated at random, and if not one of the n results is re-randomized, then the corresponding value is generated as a random result.

11. Give a method of randomly selecting m number from n number. N is large and can be thought of as billions of levels. M can be very small, such as close to 1, or it can be very large, such as near N.

A straightforward idea is to keep repeating randomly until the number of M is randomly reached. There are two drawbacks to this approach:

    1. It is difficult to get to the next random number that is already random, because the amount of data is so large that it cannot be saved in memory and is too expensive to save to external memory.
    2. If M is large, or even close to N, the numbers that follow are basically random, so there are too many random times to try.

One idea is that each number is selected probability is m/n, you can traverse the original data, while traversing each number at the same time as the probability of m/n to decide whether to select the current number, then when the traversal is complete, the selected number in the mean meaning is M. This will better approximate m with the increase of N, but it is not very precise to guarantee that the random number must be M.

Although the above ideas do not meet the requirements, but we can make improvements. Just now we are traversing each number with the same probability m/n decide whether to select the number, in fact, in the current traversal of the number in front of the result of the number we have already known, we can be based on the previous random results of dynamic adjustment of the current stochastic strategy, so that the final can be guaranteed to be the number of M.

The specific practice is to traverse the 1th number when there is m/n probability to choose, if the 1th number is selected, then the 2nd number is selected to adjust the probability of (m-1)/(n-1), if the 1th number is not selected, then the probability of the 2nd number is selected m/(n-1). That is, if you have selected K at this time, the probability of (m-k)/(n-i+1) determines whether or not to select the current I-number.

This ensures that each time you can select the appropriate number in the remaining number so that the total number of choices is M. For example, if the preceding is random m, then the probability of the subsequent random becomes 0. If there is no random number on the front, then the probability of being randomly followed is close to 1. The resulting results are always exactly m numbers.

12. Give the method of selecting 1 randomly from n number. Note that n is very large and does not know its exact value at first. The number is one for you, and when you're done, you have to give a random result immediately.

The value of n here is very large, and requires an immediate answer, so you can't save all the numbers first, and then slowly consider which one to randomly.

This question is similar to the previous one, because we don't know how many numbers there are, so we have to have a current result when we get each number, so we can give the answer when the numbers are over.

So the 1th figure is the one that must be taken. The question is, when the 2nd figure comes, do we have to keep the numbers on hand, or do we take the 2nd number now? More generally, when I (i>1) figures come, do they retain the numbers on hand or do they choose the current number I?

The answer is to ensure that each number is selected the probability is equal, when the number of I come, if we have ensured that the first i-1 number of each number of the probability of being selected is equal, so long as the number I is selected is 1/i, we can know that all I number is selected probability is 1/i. Therefore, it is only necessary to decide whether to select the current number I by the probability of 1/i.

It is guaranteed that for any n, when the number of n is given, the probability of selecting each number is equal to 1/n.

13. The method of selecting m randomly from n number is given. Note that n is very large and does not know its exact value at first. The number is one for you, and when you're done, you have to give a random result immediately.

This problem is the promotion of the previous question, so it can be modeled.

First the former m number is to be taken. The question is, when I (i>m) numbers come, are we going to discard this number or keep it? If you want to keep this number, you have to discard the number of m already in your hand, how do you know which one to discard?

The following is a concrete approach. When the number of I arrives, decide whether to choose this number by the probability of m/i. If this number is selected, replace one of the m numbers in the hand randomly.

If the first i-1 a number to ensure that each number is the same probability of being selected, then this will ensure that each number is selected in equal probability, for m/i.

    1. The probability of selecting the number of I is m/i, because the algorithm is determined as such.
    2. Consider any one of the first i-1 numbers, the probability that it is selected before the number of I is m/(i-1). At the time of the number I, this number is to be selected two possibilities, one is that the number of I have not been selected (probability is 1-m/i), and the second is the number of times I selected (probability is m/i) but the replacement of the number is not it (probability is 1-1/m), so the number of numbers in the number I was still selected probability is m/ (i-1) * ((1-m/i) + (m/i * (1-1/m))) = m/(i-1) * ((i-1)/i) = m/i.

By the principle of mathematical induction, for any n, when the number of n is given, the selected result can ensure that the n number of each selected probability is equal, for m/n.

Probability problem (I.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.