This article mainly introduces the C # implementation of red envelope function, the use of normal distribution to calculate the amount of red envelopes, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the code of C # imitation red envelopes function, for your reference, the specific content as follows
Program.cs Code:
Class Program {static void Main (string[] args) {//Initialize the red envelope base data to be initiated double total = 100; int num = 50; Double min = 0.01; string temp; BOOL flag = FALSE; Console.WriteLine (String. Format ("Whether you need to customize the red envelope amount and quantity (default {0}/{1} person) y/n:", Total, num)); temp = Console.ReadLine (); if (temp. Trim (). ToLower (). Equals ("y") | | Temp. Trim (). ToLower (). Equals ("yes")) {Console.WriteLine ("Please enter the bonus amount you want to launch:"); do {temp = Console.ReadLine (); Flag = Double. TryParse (temp, out total); if (!flag) {Console.WriteLine ("The amount must be an integer or a decimal number, please re-enter:"); }} while (!flag); Console.WriteLine ("Please enter the number of red envelopes you want to launch:"); do {temp = Console.ReadLine (); flag = Int. TryParse (temp, out num); if (!flag) {Console.WriteLine ("The number of red packets must be an integer, please reenter:"); }} while (!flag); } total = min * num; if (Total < 0) {Console.wriTeline ("Sorry, your amount is insufficient!") "); Return }//Generate a normal distribution of the random red envelope amount, and calculate the relevant amount and quantity to guarantee the accuracy of the data double average = total/num; Double variance = 1; Random u1 = new Random (); Random U2 = new Random (); double[] Nums = new Double[num]; for (int i = 0; i < num; i++) {double-result = total; if (I < num-1 && Total > 0) {do {result = Round (double) Normal (U1. Nextdouble (), U2. Nextdouble (), average, variance), 2); } while (result = = NULL | | result < 0); if (Total > Result) {total = (double) Round ((Total-(double) result), 2); } else {result = total; Total = 0; }} else if (i = = num-1) {total = 0; } Nums[i] = Math.Round (min + (double) result, 2); Floating-point arithmetic, where the data needs to be rounded to correct Console.WriteLine (string. Format ("{0} Red Envelope amount: {1}", I + 1, (min + result));Console.WriteLine ("Remaining Amount:" + ((i! = num-1 && Total = = 0)? min * (Num-i-1): All + (min * (num-i-1))); } Console.WriteLine ("Maximum amount:" + nums. Max ()); Console.WriteLine ("Minimum Amount:" + nums. Min ()); Console.WriteLine ("Total:" + Round (nums. Sum (), 2)); Console.WriteLine ("Initial variance:" + variance); Console.WriteLine ("Result variance:" + Variance (nums)); Console.WriteLine ("Press any key to exit! "); Console.readkey (); }///<summary>//produce random numbers that meet the normal distribution///</summary>//<param name= "U1" > Normal distribution First random number </param> ; <param name= "U2" > Normal distribution Second random number </param>//<param name= "Averagevalue" > Normal expectation (average) </param>// <param name= "Variance" > Normal standard deviation (math.sqrt (variance)) </param>//<returns></returns> public static D Ouble? Normal (Double U1, double U2, double Averagevalue, double variance) {Double, result = NULL; try {result = Averagevalue + math.sqrt (variance) * MATH.SQRT (( -2) * Math. Log (U1)) * Math.sin (2 * Math.PI * U2); } catch (Exception) {result = null; } return result; }///<summary>//To ask for a set of data variance///</summary>//<param name= "list" > required array </param>// /<returns></returns> public static double Variance (double[] nums) {Double average = nums. Sum ()/nums. Length; Double sum = 0; Double variance = 0; foreach (double num in nums) {sum + = Math.pow ((num-average), 2); } variance = Sum/nums. Length; return variance; }///<summary>//intercept decimals to specify decimal places and do not round///</summary>//<param name= "Originnum" > Decimals to intercept </p aram>//<param name= "Lastnum" > Intercept decimal digits </param>//<returns></returns> public static Double? Round (double originnum, int lastnum) {double? result = null; int index = originnum.tostring (). IndexOf ('. '); if (Index! =-1) {StrinG temp = originnum.tostring (); result = convert.todouble (temp. Substring (0, Index + 1) + temp. Substring (index + 1, math.min (temp. Length-index-1, Lastnum)); } if (result = = 0) {result = null; } else if (index = =-1) {result = Originnum; } return result; } }
Operation Result: