C#实现微信红包功能
程序员文章站
2023-11-21 08:58:04
本文实例为大家分享了c#仿微信红包功能的具体代码,供大家参考,具体内容如下
program.cs代码:
class program
{
stat...
本文实例为大家分享了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; } }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。