产生有序的全局唯一Id
2017-01-11 17:16
337 查看
//利用时间和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; }
相关文章推荐
- 高并发之 - 全局有序唯一id Snowflake 应用实战
- 分布式ID生成方法-趋势有序的全局唯一ID
- 【转】高并发分布式系统中生成全局唯一Id汇总
- JAVA生成全局唯一ID 使用 java.util.UUID
- MongoDB文档(Document)全局唯一ID的设计思路
- 高并发分布式系统中生成全局唯一Id汇总
- 使用redis生成全局唯一id
- 生成全局唯一id
- 分布式系统中生成全局唯一ID的方法
- 生成全局唯一ID的几种方法
- 高并发分布式系统中生成全局唯一Id汇总
- ACCESS中使用GUID全局唯一标识符的自动唯一编号[同步复制ID]之解决方法
- MD5加密以及产生唯一的ID
- 全局唯一ID的生成方式
- 全局唯一ID的生成方式
- [Java--算法]--生成全局唯一的Id(IdGen)
- C语言实现分布式自增有序的唯一ID生成算法-snowflake算法
- 全局唯一ID生成器(Snowflake ID组成)
- Mysql系列七:分库分表技术难题之分布式全局唯一id解决方案
- Twitter的分布式自增ID算法Snowflake的PHP实现,Snowflake PHP版本,高并发唯一id,全局唯一id,不重复id