Random Integer Generator

Source: Internet
Author: User
Tags integer generator

Occupy the pit first. Revise later

I met a question yesterday, Given int Rand (1) = 0 or 1-uniformly distributed, write a function to implement Rand-uniformly distribut Ed

Because the undergraduate time probability did not learn, hung. Come back online A look at the information found there are similar topics-Given rand (5) = {1, 2, 3, 4, 5}, for Rand (7).

The code to ask for Rand7 is

 Public Static int random7 () {       int val = (RANDOM5 ()-1) * 5 + (RANDOM5 ()-1);          return Val < 21? Val% 7 + 1 : Random7 ();}

The idea is actually to map 1-5 of the numbers to 1-7 of the range, as long as it is uniformly distributed on the line, we can not care whether the 1-7 probability is 4%, 2, or 1%, as long as they are equal, the surplus part of us to abandon. For example, the calculation range is 1-10.

    • We can take 1, 2, 3, 4, 5, 6, 7, Discard 8-10 (discard the method is to call once again the random function, the program may be dead loop, but from the probability of look dead cycle chance is very small)
    • You can also take 4, 5, 6, 7, 8, 9, 10, discard 1-3, but return val-3 when calculating the answer.

The above code is a solution, we can also have other solutions. such as the following,

 Public Static int random7 () {       int val = (RANDOM5 ()-1) + (RANDOM5 ()-1) * 5 + (RANDOM5 ()-1) *;          return val < 119? Val% 7 + 1 : Random7 ();}

The principle is that as long as the number of RANDOM5 () is evenly mapped to a series of more than 7 consecutive numbers.

So back to this topic, Given rand (1) implements Rand (29), where rand1 () = 0 or 1, only two numbers at a time, so we can use the following code to map to 0-31, and then discard 30 and 31:

 Public Static int rand29 () {       int val = rand1 () + RAND1 () * 2 + rand1 () * 4 + RAND1 () * 8 + rand1 () *;            return Val < 29? val:rand29 ();}

Reference:

Http://www.growingwiththeweb.com/2014/03/given-random5-implement-random7.html

http://www.careercup.com/question?id=12426697

Random Integer Generator

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.