程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#實現微信紅包功能

C#實現微信紅包功能

編輯:C#入門知識

C#實現微信紅包功能。本站提示廣大學習愛好者:(C#實現微信紅包功能)文章只能為提供參考,不一定能成為您想要的結果。以下是C#實現微信紅包功能正文


C#實現微信紅包功能

作者:WuLex

這篇文章主要介紹了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;
    }
  }

運行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved