Method One: Generate two (1,5) random number, this is a total of 25 cases, note that the two numbers are in order, from the 25 cases, take the first 21, each three kinds of representative (1,7) A number, if you take the case of the 21, discard the re-fetch.
Method Two: Generate three (1,5) random numbers, representing a bits, where 1 and 2 are mapped to 0, 3 skipped, 4 and 5 are mapped to 1. This produces a three-bit binary number, that is, 1-8 of these 8 numbers are equal probabilities. If the result is 8, then discard it.
- Rejection sampling method: Generates a random number of two (1,5), produces a two-bit five-digit number, 5 * (RANDOM5 () –1) + RANDOM5 (). This formula can generate 1-25 of the probability that the first random number represents: 0,5,10,15,20, status represents 1,2,3,4,5. So for this number (1-25 of the figure), the method of using method one, only 1-21, 7 points, representing 1-7,22-25 these 4 numbers throw away
- Code:
int random_n () { int0 ; int // T is n maximum multiple and satisfies T <= m * m Do { 1) + random_m (); while (val > t); return Val;}
- Reference:
- http://yaronspace.cn/blog/archives/1326
- Https://www.cnblogs.com/pangxiaodong/archive/2011/10/21/2220582.html
The range of random numbers, such as: Based on the (1,5) random number generator, generates a random number within (1,7)