Red Envelope algorithm

Source: Internet
Author: User

  1  /*n random number, sum is sum, and the range of each random number is [Min,max].  2 The former n-1 is randomly generated with the RAND function, and the nth number is set to val=sum-(the sum of the previous n-1). 3 (1) if Val belongs to [Min,max], it can be used directly; 4 (2) if Val>=max, use Max.  Val-max processing: In the 1~n-1 random out of a sequence number m, will be more than the Val-max to the number of M, if the complement of 200 still have the remaining, then continue to do such operations until the redundant part is allocated. 5 (3) If val<=min, then use Min. Min-val processing: In the 1~n-1 random out of a sequence number m, will be more out of the min-val portion from the number of M deducted from this, if the cut to Min is still not finished, continue this operation until the trim. */  6   7      Public classRandomgenerator8     {  9         /// <summary> Ten         ///generates a random red envelope with a specified number of limited amounts, and the total is the specified value  One         /// </summary>  A         /// <param name= "Totalvalue" >Specify Total</param>  -         /// <param name= "min" >minimum Boundary</param>  -         /// <param name= "Max" >Maximum Boundary</param>  the         /// <param name= "num" >Quantity</param>  -         /// <returns>returns the generated data as a list</returns>  -          Publiclist<Double> Randomdata (DoubleTotalvalue,DoubleMinDoubleMaxintNum out stringmessage) -         {  +             if(Min >max) -             {  +Message ="min > Max, parameter Error";  A             }  at             -list<Double> list =Newlist<Double>();  -   -             //Judging the rationality of parameters  -             if(min * num = =totalvalue) -             {  in                  for(inti =0; i < num; i++)  -                 {  tolist. ADD (min);  +                 }  -Message ="Fixedvalue";  the                 returnlist; *             }  $             if(min * num >totalvalue)Panax Notoginseng             {  -Message ="min * num > totalvalue, unreasonable Parameters";  the                 return NULL;  +             }  A             if(max * num = =totalvalue) the             {  +                  for(inti =0; i < num; i++)  -                 {  $list. ADD (max);  $                 }  -Message ="Fixedvalue";  -                 returnlist; the             }  -             if(Max * Num <totalvalue)Wuyi             {  theMessage ="Max * num < Totalvalue, unreasonable parameters";  -                 return NULL;  Wu             }  -               About             Doublesum =0.0;  $Random random =NewRandom (); -             DoubleDiffvalue = max-min; -              -             Doubletemp; A             //random generation of the first num-1 data  +              for(inti =0; I < num-1; i++)  the             {  -temp = Math.Round (random. Nextdouble () * diffvalue + min,2);  $Sum + =temp; thelist. ADD (temp);  the             }  the             //To ensure that the sum is a specified value, the generation of the last data must be processed  the             DoubleGap = totalvalue-sum; -             if(Gap >= min && Gap <=max) in             {  thelist. ADD (GAP);  theMessage ="Success";  About                 returnlist; the             }  the             Else if(Gap >max) the{//The remaining value is greater than Max  +list. ADD (max);  -Gap = gap-Max; the                 intindex;Bayi                 Doublevalue; the                  while(Gap >0)  the                 {  -index = random. Next (Num-1);//generates a random integer between 0 and num-1 (including 0, excluding num-1)  -Value =List[index]; the                     Doublemargin = max-value; the                     if(margin>0)  the                     {  the                         if(Gap >=margin) -                         {  theList[index] =Max; theGap = gap-margin; the                         } 94                         Else   the                         {  theList[index] = List[index] +Gap; theMessage ="Success"; 98                             returnlist; About                         } -                     }101                 }102             }103             Else //remaining value less than min104             { thelist. ADD (min);106                 DoubleNeed = min-Gap;107                 intindex;108                 Doublevalue;109                 Doublebuffer; the                  while(Need >0)111                 { theindex = random. Next (Num-1);//generates a random integer between 0 and num-1 (including 0, excluding num-1)113Value =List[index]; theBuffer = value-min; the                     if(Buffer >=need) the                     {117List[index] = List[index]-need;118Message ="Success";119                         returnlist; -                     }121                     Else122                     {123List[index] =min;124Need = need-buffer; the                     }126                 }127             } -Message ="Success";129             returnlist; the         }131  the        133}

Red Envelope algorithm

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.