Recently, in an induction exercise, I encountered a random number problem and sorted out the analysis process.
This article mainly discusses how to generate a random number in a wide range and the equi probability in a random range. 1. Requirement 1: generate a large random number. 2. The probability that the random number is within the random range. 2. knowledge background we know that the rand () function in C can provide random numbers. The range of the rand () function is 0 to 32727. We assume that the random number generated by rand () is equal probability in the range of 0 to 32727. If we need to obtain a random number in a small range, such as a random number between 0 and 55, we can use rand () % 55. However, to obtain a random number in a larger range, rand () cannot meet our requirements. 3. Process 1: multiply two rand values. Suppose we want to generate a random number within 1 billion. If we think that rand () can generate 0 to 32727, then we can use rand () * rand (), which is within 1 billion. However, it is not difficult to find that rand () * rand () has problems. The biggest problem is the probability of random numbers generated within the specified range. For example, a prime number greater than 32727 will never be generated. However, for a lot of aggregate numbers, the frequency will be very high. 2. In a bitwise combination, we first find the maximum number of digits, then generate a random number ranging from 0 to 9 for each digit, and combine a series of numbers ranging from 0 to 9. Assume that we want to generate a random number within 1 billion, that is, we need to generate a random number between 0 and 999999999. First, we need to obtain a random number of 9 digits, and then we will generate 9 digits, and combine them into a 9-digit number. For example, 872345671,021098652. It seems that there is no problem. We have solved a special instant range, that is, 1 billion. Suppose we want to generate a random number within 60000, that is, we need to generate a number between 0 and 59999. What should we do if we use the above method to generate a number greater than 59999 and a 5-digit number, such as 97863? 3. What we first think of the remainder method is: if the generated number is 98763) for the range of 60000) for the remainder, For a number for the remainder, the result is definitely within the range of this number. It is not difficult to find that we have a probability problem here. For numbers between 40000 and 60000, the probability of occurrence is 1/100000. For numbers between 0 and 40000, the probability of occurrence is 2/100000, so the probability is not equal. 4. In the bitwise test, we obtain the number of the upper limit number, and generate random numbers ranging from 0 to this number one by one. For random numbers between 0 and 59999, we first take the first digit: 5, And we generate a random number between 0 and 5. The second digit: 9, we generate a random number between 0 and 9, the final combination of five digits is between 0 and 59999. We found that this only solves the special Numerical range. If we want to generate a number between 0 and 51782, this method will become invalid. For example, the number 33216 cannot be generated, because the second digit 3 of 33216 is greater than the range 51782. 5. Similarly, we still use the combination method to generate a data with a specified number of digits. If we find that the number we generate is out of our range, we choose to discard the data, continue to generate random numbers until we generate random numbers within the range. It is not hard to prove that dropping an incorrect number does not affect the probability of generating the correct number. Therefore, the bitwise combination + discard method can meet our requirements. Here we only discuss the launch of random numbers. The same applies to the lower limit of random numbers. 4. Source Code Implementation
-
- // Generate a random number ranging from 0 to 9
- Static _ inline int min_rand ()
- {
- Return rand () % 10;
- }
-
- /*************************************** **********************/
- /* Function: generate a random number in the range */
- /* Parameter 1, range: the random number range */
- /* Return: returned data */
- /*************************************** **********************/
- Int my_rand (const int range)
- {
- Short bit = 0; // number of records
- Int tempt = range;
- Int rand_data = 0;
-
- While (tempt> 0)
- {
- Bit ++;
- Tempt = tempt/10;
- }
-
- While (bit --)
- Rand_data = 10 * rand_data + min_rand (); // random combination
-
- If (rand_data> = range)
- Return my_rand (range); // The generated random number does not match the range. Continue
-
- Return rand_data;
- }
This article from the "Zou Hui" blog, please be sure to keep this source http://zouhui.blog.51cto.com/3827922/869779