The range of random numbers, such as: Based on the (1,5) random number generator, generates a random number within (1,7)

Source: Internet
Author: User

  1. 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.

  2. 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
      1. 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;}

  3. Reference:
    1. http://yaronspace.cn/blog/archives/1326
    2. 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)

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.