Probability-related questions

Source: Internet
Author: User

1. a random number generator generates 0 with probability P and 1 with probability (1-p). How to generate equal probability 0 and 1?

If the random number generator is used to generate two bits, the probability of occurrence of 00 is, the probability of occurrence of 01 is, the probability of occurrence of 10 is, and the probability of occurrence of 11 is. No. The probability of 01 and 10 is equal. Then we can use this random number generator to generate two digits each time until the value is 01 or 10. When the value is 01, the output value is 0, and when the value is 10, the output value is 1.

Problem Extension: it is still required to generate such a random number generator with probability.

Solution 1: N bits are generated each time. When the first bits are 1 and the other bits are 0, 1 is output. When the second bits are 1, 0 is output 2 ,...... When only the nth bit is 1 and the other is 0, the output is N. (This solution is a little painful, a lot of information is wasted, and the complexity is too high)

Solution 2: from the answers to the original question, we can successfully obtain the Equal Probability of generating 0 and 1, and find the number of digits represented by n-1 binary, for example, K. Then we generate a K-bit binary number every time with equal probability and assemble the K-bit binary number into a number. If this number is between 0 and ~ In the n-1 range, enter this number + 1. Otherwise, it will be generated again.

2. Generation of non-repeated random numbers

We can refine this question a little and randomly generate 0 ~ K non-repeated random numbers in n-1.

Solution 1: the easiest way to think of is to use a set to hold the random numbers generated. When the size of the set is smaller than K, it will be randomly generated and added to the set. This method will not work if the shoes that have really been used this way. When K is a little bigger, thisProgramAnd then we ran out.

Solution 2: open an array a with a length of N and load 0 to it ~ N-1, then a random 0 ~ Number X of N-1, exchange a [X] and a [n-1], and output a [n-1]. Then a 0 ~ The number y of The N-2, exchange a [y] with a [N-2], and output a [N-2], know the number of K output ......CodeSee here for implementation.

3. Given a function that can generate integers 1 to 5 at random, write a function that can generate integers 1 to 7 at random.

Solution 1:

Number of K generation (k> 1) is assumed to be N1, N2, N3, n4... so the number of definitions produced is n1-1 + (n2-2) * 5 + (n3-1) * 5 ^ 2 + (n4-1) * 5 ^ 3 ........ the resulting digit is in the range (0, 5 ^ k-1 ). Then, divide 5 ^ K into K and the generated digit is the random number (0 ~ 6), and then + 1. If it is in the remainder of the K-score range, the above process is re-executed. Don't worry about the remainder problem. When k gets 3, the probability of falling into the remainder range has been reduced to 6/125, and the remainder will not cause the probability problem, but will affect the efficiency. (Equivalent to 5 hexadecimal)

Solution 2:

Use rand5 () * 5 + rand5 () to generate 6 7 8 9 10 11 ...... 26 27 28 29 30 these 25 numbers, each of which has the same probability, go to the first 3*7 number, discard the next 4 numbers, convert 6 7 8 to 1, 9 10 11 to 2 ,......, Formula: (A-3)/3, in fact with the solution 1 is the same, just wrap it.

4. How to randomly select 1000 keywords

A given data stream contains endless search keywords (for example, keywords that people continuously enter during Google search ). How can we randomly select 1000 keywords from this endless stream?

Defines an array with a length of 1000. The first 1000 keywords in the data stream must be put in the array. For the n (n> 1000) keyword in the data stream, we know that the probability of this keyword being randomly selected is 1000/n. Therefore, we use this keyword to replace the random one in the array with a probability of 1000/n. This ensures that all keywords are selected with a probability of 1000/n. We can make sure that 1000 random keywords are always stored in the array.

PS: About the probability of 1000/N, We can randomly generate 1 ~ The number of N ~ Replace the corresponding location within 1000.

5. Randomly select a point from the circle with a radius of 1.

Solution 1:

Assume that the center is (0, 0 ). Select a random point in the square of the X axis [-1, 1] and Y axis [-1, 1. Then, determine whether the vertex is in the circle (by calculating the distance from this vertex to the center of the circle ). If it is in the circle, this point is the result. If it is not in the circle, it is re-selected until it is found. The square area is 4, and the circle area is pi. Therefore, the probability of random points in the Square within the circle is PI/4.

Solution 2:

Select an angle randomly from [0, 2 * PI), corresponding to a radius in the circle, and then select a point on the radius. However, the points on the radius cannot be evenly selected. The selected probability should be proportional to the length from the center of the circle, so that the random points are evenly distributed within the circle.

About the problem of uneven Selection: a point is randomly selected on the oblique side of a right triangle (the length of the oblique side PI and the length of the straight angle Side 1), and the projection to the straight angle side with the length of 1 should meet the conditions.

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.