zoukankan      html  css  js  c++  java
  • Redis抢红包

    static void Main(string[] args)
            {
                //1.
                double totalAmount = 100;
                int num = 10;
                double minAmount = 0.01;
                RedisClient client = new RedisClient("127.0.0.1", 6379);
                Queue<double> qu = new Queue<double>();
                Random r = new Random();
                for (int i = 1; i < num; i++)
                {
                    //
                    //(总金额-(总人数-第一个人)*最小金额)/(总人数-第一个人)
                    //安全金额
                    double safeAmount = (totalAmount - (num - i) * minAmount) / (num - i);
    
                    double money = NextDouble(r, minAmount * 100, safeAmount * 100) / 100;
                    money = Math.Round(money, 2, MidpointRounding.AwayFromZero);
                    totalAmount = totalAmount - money;
                    totalAmount = Math.Round(totalAmount, 2, MidpointRounding.AwayFromZero);
    
                   qu.Enqueue(money);
                 //   Console.WriteLine("第" + i + "个红包:" + money + " 元,余额:" + totalAmount + " 元");
                }
               // Console.WriteLine("第" + num + "个红包:" + totalAmount + " 元,余额:0 元");
                client.Add<Queue<double>>("key", qu);
                //模拟,当点击抢
                Queue<double> que = client.Get<Queue<double>>("key");
                if (que.Count > 0)
                {
                    double am = que.Dequeue(); 
                    var t = new { name = "123213",amount= am ,date=DateTime.Now};
                   
                }
                else
                {
                    //没了
                }
                //if (qu.Count > 0)
                //{
                //    Queue<double> que = client.Get<Queue<double>>("honbao");
                //    //出红包
                //    que.Dequeue();
                //}
                //else { 
                ////红包没了;
                //}
                Console.ReadKey();
            }
    
            /// <summary>
            /// 生成设置范围内的Double的随机数
            /// eg:_random.NextDouble(1.5, 2.5)
            /// </summary>
            /// <param name="random">Random</param>
            /// <param name="miniDouble">生成随机数的最大值</param>
            /// <param name="maxiDouble">生成随机数的最小值</param>
            /// <returns>当Random等于NULL的时候返回0;</returns>
            protected static double NextDouble(Random random, double miniDouble, double maxiDouble)
            {
                if (random != null)
                {
                    return random.NextDouble() * (maxiDouble - miniDouble) + miniDouble;
                }
                else
                {
                    return 0.0d;
                }
            }
  • 相关阅读:
    条件运算符
    类型
    c#
    打印菱形
    关于隐藏控制器的导航条的问题
    怎么去掉Xcode工程中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int32
    如何在导航条的button点击变换时,切换对应的控制器
    如何只选择一个
    重写TabBar遇到的按钮不显示的问题
    ASI和AFN的区别
  • 原文地址:https://www.cnblogs.com/ntg2/p/13215768.html
Copyright © 2011-2022 走看看