zoukankan      html  css  js  c++  java
  • 产生有序的全局唯一Id

    //利用时间和Guid作为种子产生随机数
    private string GenerateUniqueID()
    {
          System.Text.StringBuilder sb = new System.Text.StringBuilder();
          string str1 = DateTime.Now.ToString("yyyyMMddHHmmss");
          Random d = new Random(Guid.NewGuid().GetHashCode());
          string str2 = d.Next(10000, 100000).ToString();
          return sb.Append(str1).Append(str2).ToString();
     }
      
           //利用时间和RNGCryptoServiceProvider产生
            public static string GenerateOrderNumber()
            {
                string strDateTimeNumber = DateTime.Now.ToString("yyyyMMddHHmmssms");
                string strRandomResult = NextRandom(1000, 1).ToString();
                return strDateTimeNumber + strRandomResult;
            }
    
            /// <summary>
            /// 参考:msdn上的RNGCryptoServiceProvider例子
            /// </summary>
            /// <param name="numSeeds"></param>
            /// <param name="length"></param>
            /// <returns></returns>
            private static int NextRandom(int numSeeds, int length)
            {
                // Create a byte array to hold the random value.  
                byte[] randomNumber = new byte[length];
                // Create a new instance of the RNGCryptoServiceProvider.  
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                // Fill the array with a random value.  
                rng.GetBytes(randomNumber);
                // Convert the byte to an uint value to make the modulus operation easier.  
                uint randomResult = 0x0;
                for (int i = 0; i < length; i++)
                {
                    randomResult |= ((uint)randomNumber[i] << ((length - 1 - i) * 8));
                }
                return (int)(randomResult % numSeeds) + 1;
            }
    

      

  • 相关阅读:
    VOIP开源项目源码地址(一)
    iptables下udp穿越实用篇
    function socket about http://net.pku.edu.cn/~yhf/linux_c/function/14.html
    IOKE的SIP协议专栏
    XviD core API overview: Decoding
    Socket about
    sql海量数据优化
    Socket、多线程、消息队列、共享资源并发下的性能研究
    【转】SQL 索引理解
    SQL 索引理解
  • 原文地址:https://www.cnblogs.com/jeffrey77/p/6274454.html
Copyright © 2011-2022 走看看