zoukankan      html  css  js  c++  java
  • 红包算法

    1.红包算法-封装方法

         /// <summary> 

            ///  微信红包算法 

            /// </summary> 

            /// <param name="allmoney">红包金额</param>

            /// <param name="moneycount">红包数量</param>

            /// <returns></returns>

            public static string GetMoneys(double allmoney, int moneycount) 

            { 

                string retamt = ""; 

                //人均最小金额 

                double min = 0.01; 

                if (allmoney < moneycount * min) 

                    return null; 

                        int num = moneycount; 

                List<double> array = new List<double>(); 

                Random r = new Random(); 

                for (int i = 0; i < num; i++) 

                {

                    if (moneycount == 1) 

                    {

                         moneycount--; 

                        array.Add(Convert.ToDouble(allmoney.ToString("0.0000"))); 

                        if (retamt.Length > 0) 

                        { 

                            retamt += ","; 

                        } 

                        retamt += Convert.ToDouble(allmoney.ToString("0.000")).ToString();     

                    }else 

                    {

                        //保存剩余金额可以足够的去分配剩余的红包数量 

                        double max = (allmoney - (moneycount - 1) * min) / moneycount * 2; 

                        double money = r.NextDouble() * max; 

                        money = Convert.ToDouble((money <= min ? min : money).ToString("0.0000")); 

                        moneycount--; 

                        allmoney -= money; 

                        array.Add(money); 

                        if (retamt.Length > 0) 

                        {

                            retamt += ","; 

                        } 

                        retamt += money.ToString();      

                    } 

                } 

                string ret = string.Join(",", retamt); 

                //再次随机 

                return ret; 

            } 

    2.调用

       //总金额66元红包个数3个

        string money= GetMoneys(66, 3);

                Response.Write("返回值:" +money);

  • 相关阅读:
    Ubuntu 12.10 安装破解sublimetext 2
    封装一个类型转换的方法
    【转】CultureInfo中重要的InvariantCulture
    C# 36进制转10进制
    用泛型的IEqualityComparer<T>接口去重复项
    关于wamp启动是80端口被占用的问题详解(win7系统下WAMP 80端口被MicrosoftHTTPAPI/2.0占用的解决办法)
    Ubuntu 更改鼠标滚轮速度
    edge ctrl+c 复制不起作用
    centos 7 mysql8 安装和卸载
    Windows Terminal 终端 SSH连接centos7 linux
  • 原文地址:https://www.cnblogs.com/dsjbk/p/12664560.html
Copyright © 2011-2022 走看看