Fourth chapter: Random number

Source: Internet
Author: User

1. Answer to question 2nd

Once the corresponding subscript for T is found, continue to find the equal element to the left linearly until an unequal element is encountered.

2. Answer to question 6th

1.1 Conclusion

Each time it is executed, the number of beans in the jar is subtracted by 1, so this process can be terminated. If the number of white beans at the beginning is odd, then the last one left is the white bean, or the black bean. (source)

1.2 Encoding Verification

#include <iostream>#include<vector>#include<algorithm>#include<random>using namespacestd;enumcolor{Black, white};unsigned rand (intN) {uniform_int_distribution<unsigned> U (0, N); StaticDefault_random_engine E; returnU (e);}voidLastbean (vector<color> &bean1,vector<color> &bean2) {Vector<color> Bean (bean1.size () +bean2.size ());    Merge (Bean1.begin (), Bean1.end (), Bean2.begin (), Bean2.end (), Bean.begin ());    unsigned i, J;  while(Bean.size () >1) {i= rand (Bean.size ()-1); J= rand (Bean.size ()-1);  while(J = =i) {J= rand (Bean.size ()-1); }        if(Bean[i] = =Bean[j]) {            if(i<j)            {Swap (I, j); } bean.erase (Bean.begin ()+i); Bean.erase (Bean.begin ()+j);        Bean.push_back (black); }        Else{            if(Bean[i] = =Black) {Bean.erase (Bean.begin ()+i); }            Else{bean.erase (Bean.begin ()+j); } }} cout<<bean[0] <<Endl;}intmain () {vector<color> Bean1 ( One, Black); Vector<color> Bean2 ( A, Black); Vector<color> Bean3 ( One, white); Vector<color> Bean4 ( A, white); Lastbean (Bean1, bean3);//There were odd white beans at the beginning .Lastbean (bean2, BEAN3); Lastbean (Bean1, bean4);//There were even white beans at the beginning .Lastbean (bean2, BEAN4); System ("Pause"); return 0;}
View Code

Operation Result: (0-> black,1-> white)

1.3 Random number problem

Because you want to randomly select two beans, you need to generate a pair of unequal random numbers based on a random number range. Here's how:

    1. Different distributions for different n need to be generated
    2. New random number to be generated for the same n (i.e. the same distribution)

The resulting random function is as follows:

unsigned rand (int  n) {    uniform_int_distribution<unsigned> u (0, n);     Static default_random_engine e;     return U (e);}

Explian:

unsigned rand (int  n) {    uniform_int_distribution<unsigned> u (0, n);    Default_random_engine e;     return U (e);}

For the above code, the random number engine E generates a random sequence (which may contain the same number), and the Constant invocation of U (e) generates a sequence of random numbers between [0,n] (which may contain the same number as the sequence s), and the problem is that each call to the function rand (n) returns the first number in S, This is not what we want, so we need to add a static to Default_random_engine E, so that e will remain in the state between the function rand (n) calls, that is, calling rand (n) returns the random number in s in turn. At this point, condition 2 has been satisfied. To satisfy the condition 1,uniform_int_distribution<unsigned> u (0,n) before the static can be. Sometimes two places have to be static (see C + + Primer Chinese version of P662).

This does not guarantee that a pair of random numbers generated by the same n is not equal, write the following code to solve:

11);  while (j = = i)    {1);}

Fourth chapter: Random number

Related Article

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.