C # random Red envelope algorithm

Source: Internet
Author: User

1 Static voidMain (string[] args)2         {3             DoubleTotalAmount = -;4             intnum =Ten;5             DoubleMinamount =0.01;6Random r =NewRandom ();7              for(inti =1; i < num; i++)8             {9                 DoubleSafeamount = (TotalAmount-(num-i) * minamount)/(Num-i);Ten                 //Double money = new Random (). Next (Convert.ToInt32 (Minamount *), Convert.ToInt32 (Safeamount * 100))/100;  One                 DoubleMoney = Nextdouble (r, Minamount * -, Safeamount * -) / -; AMoney = Math.Round (Money,2, Midpointrounding.awayfromzero); -totalamount=totalamount-Money ; -TotalAmount = Math.Round (TotalAmount,2, Midpointrounding.awayfromzero); theConsole.WriteLine ("Section"+ i +"a red envelope:"+ Money +"yuan, Balance:"+ TotalAmount +"Yuan"); -             } -Console.WriteLine ("Section"+ num +"a red envelope:"+ TotalAmount +"yuan, balance: 0 Yuan"); -  + Console.readkey (); -         } +  A         /// <summary> at         ///generates a random number of double in the set range -         ///Eg:_random. Nextdouble (1.5, 2.5) -         /// </summary> -         /// <param name= "random" >Random</param> -         /// <param name= "minidouble" >generate the maximum number of random values</param> -         /// <param name= "maxidouble" >generate minimum values for random numbers</param> in         /// <returns>returns 0 when random equals null;</returns> -         protected Static DoubleNextdouble (Random random,DoubleMinidouble,Doublemaxidouble) to         { +             if(Random! =NULL) -             { the                 returnRandom. Nextdouble () * (maxidouble-minidouble) +minidouble; *             } $             ElsePanax Notoginseng             { -                 return 0.0d; the             } +}

C # random Red envelope algorithm

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.