zoukankan      html  css  js  c++  java
  • new Random().Next(1, 100); 多线程同时执行结果很高概率相同,

    /// <summary>
        /// new Random().Next(1, 100); 多线程同时执行结果很高概率相同,
        /// 是用的当前时间为seed,时间相同结果相同
        /// 
        /// 解决随机数重复问题
        /// 同时模拟远程请求的随机延时
        /// </summary>
        public class RandomHelper
        {
            /// <summary>
            /// 随机获取数字并等待1~2s
            /// </summary>
            /// <returns></returns>
            public int GetRandomNumberDelay(int min, int max)
            {
                Thread.Sleep(this.GetRandomNumber(500, 1000));//随机休息一下
                return this.GetRandomNumber(min, max);
            }
    
    
            /// <summary>
            /// 获取随机数,解决重复问题
            /// </summary>
            /// <param name="min"></param>
            /// <param name="max"></param>
            /// <returns></returns>
            public int GetRandomNumber(int min, int max)
            {
                Guid guid = Guid.NewGuid();//每次都是全新的ID
                string sGuid = guid.ToString();
                int seed = DateTime.Now.Millisecond;
                for (int i = 0; i < sGuid.Length; i++)
                {
                    switch (sGuid[i])
                    {
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                        case 'g':
                            seed = seed + 1;
                            break;
                        case 'h':
                        case 'i':
                        case 'j':
                        case 'k':
                        case 'l':
                        case 'm':
                        case 'n':
                            seed = seed + 2;
                            break;
                        case 'o':
                        case 'p':
                        case 'q':
                        case 'r':
                        case 's':
                        case 't':
                            seed = seed + 3;
                            break;
                        case 'u':
                        case 'v':
                        case 'w':
                        case 'x':
                        case 'y':
                        case 'z':
                            seed = seed + 3;
                            break;
                        default:
                            seed = seed + 4;
                            break;
                    }
                }
                Random random = new Random(seed);
                return random.Next(min, max);
            }
        }
    

      

  • 相关阅读:
    改写promise并添加超时处理
    js将文案复制到剪贴板
    学习笔记(安装、命名实体识别、BERT、面试)
    读书笔记——安装
    Markdown使用
    奔波三载,虽死犹生
    .net工程师的利器
    .NET开发相关技术
    两行代码教你用React useContext代替React-redux
    记解决 `antd is not defined` 解决ant design 打包体积过大的问题
  • 原文地址:https://www.cnblogs.com/soundcode/p/10762360.html
Copyright © 2011-2022 走看看