Random Number range extension (such as rand7 () to rand10 ())

Source: Internet
Author: User

Question:
It is known that there is a rand7 () function, returns a random natural number from 1 to 7, and uses this rand7 () to construct rand10 () random 1 ~ 10.
Analysis: To ensure the even distribution of rand10 () on integers 1-10, you can construct a random integer interval with a 1-10 * n uniform distribution (n is any positive integer ). Assume that x is a random integer in the range of 1-10 * n. Then, x % 10 + 1 is an integer evenly distributed between 1 and 10. Because (rand7 ()-1) * 7 + rand7 () can construct a random number evenly distributed between 1 and 49 (for the reason, see the following description), you can set ~ If such a random number is removed, 1-40 is still evenly distributed among 1-40, because each number can be considered as an independent event.
The following explains why (rand7 ()-1) * 7 + rand7 () can create a random number evenly distributed between 1 and 49:
First, rand7 ()-1 gets a discrete integer set {0, 1, 2, 3, 4, 5, 6}, where each integer has a probability of 1/7. Then (rand7 ()-1) * 7 gets A discrete Integer Set A = {1/7, 42}, where each integer has A probability. The probability of each integer in the Set B = {1, 2, 3, 4, 5, 6, 7} obtained by rand7 () is also 1/7. Obviously, the combination of any two elements in A and B can correspond One to One integer between 1 and 49, that is, any number between 1 and 49, A combination of two elements in A and B can be uniquely identified. Since elements A and B can be considered as independent events, according to the probability formula P (AB) = P (A) P (B) of independent events ), the probability of each combination is 1/7*1/7 = 1/49. Therefore, the integers (rand7 ()-1) * 7 + rand7 () are evenly distributed between 1 and 49, and the probability of each number is 1/49.

        x =                x =  * (rand7() - ) +     }(x >       x %  +  }

: Why not use while (x> 40) instead of while (x> 10? The reason is that if you use while (x> 10), there is a 40/49 probability that you need to loop while, and it is very likely that there is an endless loop.

It is known that the random number generator random3 () generates a random number in the range of [1, 3]. Please use random3 () to construct the random5 () function to generate a random number of [1, 5?
Problem Analysis:
How can we construct a larger range of numbers from the range [1-3? At the same time, the probability of occurrence of Numbers meeting the larger range is the same. Two possible operations can be considered: Addition and multiplication.
Consider the following expression:
3*(random3 ()-1) + random3 ();
It can be calculated that the range of the above expression is [1, 9] and the probability of occurrence of the number is the same, that is, 1/9
How can we generate the number of [1, 5] From the number in the range of [1, 9?
You can think of the rejection sampling method, that is, to generate a random number of [1, 9]. If the number range is not within [1, 5], re-sampling

 

        val =                val =  * (random3() - ) +     }(val >       }

 


Abstract This problem further. It is known that the range of the random number generator of random_m () is [1, m]. Calculate random_n () to generate a function in the range of [1, n, m <n & n <= m * m

 

        val =       t;                  val = m * (random_m() - ) +     }(val >      }

 

 

 

 

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.