(algorithm) about the generation of random numbers

Source: Internet
Author: User

Topic:

1, a function fun () evenly returns 0 and 1, the probability of each is 50%, the use of this function to implement a function, so that the probability of return 1 is 3/4,0 probability of 1/4.

2, if there is a function rand5 can equal the probability of generating an integer between 1-5, how to use Rand5 to achieve Rand7? The requirement of the Rand7 function is to be able to equal the probability of generating an integer between 1-7.

Ideas:
    • Random functions of C + + rand () and Srand ():

Where is the header file:<stdlib.h>

function one: int rand (void);
Returns a random integer between [seed, Rand_max (0X7FFF)), starting with the seed specified in Srand (seed).

function two: void Srand (unsigned seed);
The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand ().

If you want Rand () to produce a different value each time the program runs, you must give seed a variable value in Srand (seed), which must be different every time the program runs (such as the elapsed time so far), Srand ((unsigned) times (NULL ));

    • Topic 1:

There are many ideas,

For example Fun () +fun (), 0,1 add can get 3 values, 0+0=0;0+1=1;1+0=1;1+1=2, 0 is divided into two categories, equal to 0 is 0, greater than 0 is 1, the probability of the new 0 is 1/4, the probability of the new 1 is 3/4;

For example Fun () *fun (), 0,1 multiply can get 2 values, 0*0=0;0*1=0;1*0=0;1*1=1, also divided into two categories, equal to 0 is 1, equals 1 is 0, then the probability of the new 0 is 1/4, the probability of the new 1 is 3/4;

There are other ways, such as POW (fun (), fun ()), Fun ()-fun (), and so on, as long as you can find a 1:3 demarcation.

    • Topic 2:

The second problem can not be directly through the RAND5 () basic operation to obtain a uniform distribution of 1-7, such as Rand5 () +rand5 () is less than 1-10 of the uniform distribution. The probability of generating 6 (2+4,4+2,3+3) is greater than the probability of generating 1 (0+1,1+0).

Since the results cannot be obtained by operation, we can construct a larger range of data, such as multiples of 7. How to construct it? by two Rand5 () to generate two independent distributions, independent means not repeating, it can easily be thought through (Rand5 ()-1) and Rand5 ()-one to generate two independent uniform distribution, the first generation of 0,5,10,15,20 uniform distribution, the second generation 0, The uniform distribution of the 1,2,3,4, adds the two, namely (Rand5 ()-1) *5+rand5 () 1 can get 0-24 of a uniform distribution, take 7 times the number of numbers, such as 21, or 0-20, plus 1 to get a uniform distribution of 1-21, and then% 7 Get Rand7 ().

Remember that you cannot do this through Rand5 () *6-6, because we need two separate distributions.

There is also a method, from the perspective of the binary, to generate 1-7 uniform distribution, in the binary, only three bits, if each bit of 0,1 generation is also uniform (through RAND5 () can be obtained), then you can get 0-7 of the uniform distribution, generated 0 o'clock not considered that can be 1-7 evenly distributed , specific reference code.

Code:

Topic 1:

#include <iostream> #include <stdlib.h> #include <time.h>using namespace Std;int fun () {    return rand ()%2; int fun2 () {    int i=fun () +fun ();    if (i>0)        return 1;    else        return 0;} /*int fun2 () {    int i=fun () *fun ();    if (i==0)        return 1;    else        return 0;} */int Main () {    int sum_1=0;    int sum_0=0;    Srand ((unsigned) time (0));    For (long long i=0;i<999999;i++) {        if (fun2 () ==1)            sum_1++;        else            sum_0++;    }    cout<<sum_1<< ":" <<sum_0<<endl;    cout<< (float) sum_1/sum_0<<endl;    return 0;}

  

Topic 2:

#include <iostream> #include <stdlib.h> #include <time.h>using namespace Std;int rand5 () {    return Rand ()%5+1;} Generate 0,1int rand01 () {    int i=rand5 ();    if (i>4)        return rand01 ();    return i%2;} int Rand7_1 () {    int i;    while (true) {        //Generate 0-24        i= (Rand5 ()-1) *5+rand5 ()-1;        if (i<21)            return i%7+1;    }} int rand7_2 () {    int i;    Generate 0-7    i= (rand01 () <<2) + (RAND01 () <<1) +rand01 ();    if (i==0)        i=rand7_2 ();    return i;} int main () {    srand ((unsigned) time (0));    int a[7]={0,0,0,0,0,0,0};    int b[7]={0,0,0,0,0,0,0};    For (long long i=0;i<99999999;i++) {        a[rand7_1 () -1]++;        B[rand7_2 () -1]++;    }    for (int i=0;i<7;i++)        cout<<a[i]<< "";    cout<<endl;    for (int i=0;i<7;i++)        cout<<b[i]<< "";    cout<<endl;    return 0;}

(algorithm) about the generation of random numbers

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.