這篇文章主要介紹了C#實現紅包功能,使用常態分佈計算紅包金額,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文執行個體為大家分享了C#仿紅包功能的具體代碼,供大家參考,具體內容如下
Program.cs代碼:
class Program { static void Main(string[] args) { //初始化要發起的紅包基礎資料 double total = 100; int num = 50; double min = 0.01; string temp; bool flag = false; Console.WriteLine(string.Format("是否需要自訂紅包金額和數量(預設{0}元/{1}人)Y/N:", total, num)); temp = Console.ReadLine(); if (temp.Trim().ToLower().Equals("y") || temp.Trim().ToLower().Equals("yes")) { Console.WriteLine("請輸入你要發起的紅包金額:"); do { temp = Console.ReadLine(); flag = double.TryParse(temp, out total); if (!flag) { Console.WriteLine("金額必須為整數或小數,請重新輸入:"); } } while (!flag); Console.WriteLine("請輸入你要發起的紅包個數:"); do { temp = Console.ReadLine(); flag = int.TryParse(temp, out num); if (!flag) { Console.WriteLine("紅包個數必須為整數,請重新輸入:"); } } while (!flag); } total -= min * num; if (total < 0) { Console.WriteLine("抱歉,你的金額不足!"); return; } //產生常態分佈的隨機紅包金額,並計算相關的金額和數量保證資料的準確性 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); //浮點運算問題,這裡需要四捨五入資料才正確 Console.WriteLine(string.Format("第{0}個紅包金額:{1}", i + 1, (min + result))); Console.WriteLine("剩餘金額:" + ((i != num - 1 && total == 0) ? min * (num - i - 1) : total + (min * (num - i - 1)))); } Console.WriteLine("最大金額:" + nums.Max()); Console.WriteLine("最小金額:" + nums.Min()); Console.WriteLine("總額:" + Round(nums.Sum(), 2)); Console.WriteLine("初始方差:" + variance); Console.WriteLine("結果方差:" + Variance(nums)); Console.WriteLine("按任意鍵退出!"); Console.ReadKey(); } /// <summary> /// 產生符合常態分佈的隨機數 /// </summary> /// <param name="u1">常態分佈第一個隨機數</param> /// <param name="u2">常態分佈第二個隨機數</param> /// <param name="averageValue">正態期望(平均值)</param> /// <param name="variance">正態標準差(Math.Sqrt(方差))</param> /// <returns></returns> public static double? 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> /// 求一組資料的方差 /// </summary> /// <param name="list">要求的數組</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> /// 截取小數指定小數位,且不四捨五入 /// </summary> /// <param name="originNum">要截取的小數</param> /// <param name="lastNum">截取小數後位元</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; } }
運行結果: