Several random questions

Source: Internet
Author: User

1. It is known that there is a rand7 () function, and returns a random natural number from 1 to 7. How to use this rand7 () to construct rand10 (), random 1 ~ 10.

The main principle for generating random numbers is that the probability of each number is equal. If you can obtain a set of Equal Probability numbers, you can find the ing between 1 and 1 ~ 10.

Rand7 () returns 1 ~ The Natural Number of 7 to construct a new function (rand7 ()-1) * 7 + rand7 (). This function will randomly generate 1 ~ The Natural Number of 49. The reason is: 1 ~ Each number in 49 only represents the unique value of the first rand7 () and the value of the second rand7 (), so the probability of their appearance is equal.

However, there are too many numbers here. You can discard 41 ~ 49 ~ The number 40 is divided into 10 groups, each of which is mapped to 1 ~ You can get random results.

The specific method is to use (rand7 ()-1) * 7 + rand7 () to generate a random number X. If it is greater than 40, it continues to be random until it is less than or equal to 40. If it is less than or equal to 40, the random number is (x-1)/4 + 1.

2. It is known that there is a randm () function, and returns a random natural number from 1 to M. How to use this randm () to construct randn (), random 1 ~ N.

Question extension.

If n is less than or equal to M, you can directly obtain it.

When N> M, like construction (randm ()-1) * m + randm (), 1 ~ can be generated ~ M ^ 2 (randm ^ 2). You can select n constructors 1 ~ in m ^ 2 ~ N ing.

If M ^ 2 still does not have n values, you can continue construction of randm ^ 2 until the construction is successful.

3. We have known a random generator. The probability of generating 0 is P, and the probability of generating 1 is 1-P. Now we want to construct a generator so that the probability of generating 0 and 1 is 1/2.

Consider generating two random numbers consecutively. There are only four possible results: 00, 01, 10, and 11. The probability of generating 01 and generating 10 is equal, all of which are p * (1-p ), therefore, we can use this property with equal probability to generate a random number of 01.

For example, 01 ing 01 to 1. The overall solution is:

Two random numbers are generated. If the result is 00 or 11, the random number is discarded. If the result is 01, 0 is generated. If the result is 10, 1 is generated.

4. We have known a random generator, and the distribution of the generated numbers is unclear. Now we want you to construct a generator so that the probability of generating 0 and 1 is 1/2.

The idea is similar. Considering that two random numbers A and B are generated consecutively, there are three possible results: a = B, A> B, A <B. Because of the symmetry of A and B, a> B and A <B have the same probability. Therefore, a random number 01 can be generated with the same probability. The method is similar.

Alternatively, you can find another event with the same probability. For example, if you select a threshold th and divide the random number result into two conditions: smaller than the threshold and greater than or equal to the threshold, two random numbers are generated consecutively, one of them is smaller than the threshold, and the other is equal to or greater than the threshold. Then a random number is generated.

5. A random generator is known. The probability of generating 0 is P, and the probability of generating 1 is 1-P. A generator is constructed so that the probability of constructing 1, 2, and 3 is 1/3 ;.... More generally, construct a generator so that it constructs 1, 2, 3 ,... The probability of N is 1/N.

Now we know that to generate a random number with a medium probability of N, the key is to find N or more events with the same probability, and then we generate events at random, if the event is different from the N events, it is ignored directly until one of the N events is generated, and then a random number matching the event is generated. Because n events have equal probability, the probability of random numbers is equal.

Considering the Continuous Generation of X random numbers, the result should be a combination of x 0 and 1. In order to make some results have the same probability, we should make the number of occurrences of 0 and 1 in this result equal, that is, each account for half. Therefore, the length of X must be an even number. For convenience, two or more random numbers are generated consecutively. Each result with half of 0 and 1 can be assigned a certain number of 1 to n. To represent the N numbers, the sum of half of each result with 0 and 1 must be greater than or equal to n, that is

C (2 * x, x)> = N

The minimum X is the most efficient X.

Then, the first N 0 and one half of the results are given the values from 1 to n. A Random Number of 2 x is generated consecutively. If not one of the N results, the random number is re-generated. If yes, the corresponding value is generated as the random result.

6. The method for randomly selecting M numbers from N numbers is provided. N is large and can be considered as hundreds of millions. M can be very small, such as close to 1; it can also be very large, such as close to n.

A direct idea is to keep repeating the random until the random number reaches M. This method has two drawbacks:

  1. It is difficult to determine whether a random number is included in the previous step. Because the data volume is large, it cannot be stored in the memory. If it is stored in the external store, the time is too large.
  2. If M is very large, or even close to N, then the numbers that follow the random order are basically all random. Therefore, too many random attempts are required.

One idea is that the probability of each number being selected is M/N, and the original data can be traversed, when traversing each number, the probability of M/N determines whether to select the current number. When the traversal is complete, the selected number indicates m on average. This will be closer to M as N increases, but it cannot be very accurate to ensure that random numbers must be M.

Although the above ideas cannot meet the requirements, we can make improvements. When traversing each number, we decided whether to select the number with the same probability M/N. In fact, we already know the result of the number before the number is traversed. We can dynamically adjust the current random policy based on the previous random result, in this way, the random numbers must be M.

The specific method is to select the M/N probability when traversing 1st numbers. If 1st numbers are selected, the probability of 2nd numbers being selected is adjusted m-1) /(n-1). If no 1st digits are selected, the probability of the 2nd digits being selected is m/(n-1 ). That is, when the number I is traversed, if K is selected, the value is (m-k)/(n-I + 1) the probability determines whether to select the current I-th number.

This ensures that the appropriate number can be selected for the remaining number each time so that the total number selected is M. For example, if M is already random, then the random probability of the next is 0. If no random number is obtained before, the probability of random numbers is close to 1. The final result is always accurate to M numbers.

7. the random selection method from N is given. Note that N is very large and does not know its specific value at first. Numbers are given to you one by one. After the numbers are given, you must immediately give random results.

Here the value of N is very large and the answer must be given immediately. Therefore, we cannot save all the numbers first and then consider which one should be random.

This is similar to the above question, because we do not know how many numbers there are, so we must have a current result when getting each number, in this way, you can give an answer when the number is given.

Therefore, 1st digits are required. The question is, when the number is 2nd, whether to keep the number in hand or take the current number 2nd? More generally, when I (I> 1) is the number in the hand retained, or is the current I number selected?

The answer is to ensure that the probability of each number is equal, when the number of I, if we have ensured that the number of I-1 before each number is selected probability is equal, so long as the probability of the I number being selected is 1/I, we can know that the probability of all I numbers being selected is 1/I. Therefore, you only need to determine whether to select the current I-th number based on the probability of 1/I.

Therefore, we can ensure that for any N, when n numbers are given, the probability of selecting each number is equal to 1/N.

8. The random selection of M numbers from N numbers is given. Note that N is very large and does not know its specific value at first. Numbers are given to you one by one. After the numbers are given, you must immediately give random results.

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

First, the first M digits are required. The question is, when I (I> m) digits come, will this number be discarded or retained? If you want to retain this number, you must discard the number of M in your hands. How can you determine which one to discard?

The specific practices are as follows. When the number of I comes, the probability of M/I determines whether to select this number. If this number is selected, one of the M numbers in the hand is randomly replaced.

If the probability of each number being selected is equal when the previous I-1 is a number, then the probability of each number being selected is also equal to M/I.

  1. The probability of selecting the number of I is m/I, because the algorithm determines this.
  2. Consider any one of the first I-1 numbers, and the probability it is selected before the number of I is m/(I-1 ). When the number I is selected, there are two possibilities. One is that the number I is not selected (the probability is 1-m/I ), second, the number of times I is selected (the probability is m/I), but the replaced number is not (the probability is 1-1/M ), so the probability that this number is still selected in the I number is m/(I-1) * (1-m/I) + (M/I * (1-1/m) = M/(I-1) * (I-1)/I) = M/I.

According to the mathematical induction principle, when n is given to any N, the selected result ensures that the probability of each selected N is equal, is M/N.

Several random questions

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.