UtilRandom.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. namespace CommonTool
  3. {
  4. public class UtilRandom
  5. {
  6. static Random _random;
  7. public static int GetRandomSeed()
  8. {
  9. byte[] bytes = new byte[4];
  10. System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
  11. rng.GetBytes(bytes);
  12. return BitConverter.ToInt32(bytes, 0);
  13. }
  14. public static int GetIntRandom()
  15. {
  16. if (_random == null)
  17. _random = new Random(0x989680);
  18. return _random.Next(0x989680, 0x5f5e0ff);
  19. }
  20. public static string GetRandom()
  21. {
  22. if (_random == null)
  23. _random = new Random(0x989680);
  24. int liInt = _random.Next(0x989680, 0x5f5e0ff);
  25. return ("R" + liInt.ToString());
  26. }
  27. public static string GetRandom1()
  28. {
  29. if (_random == null)
  30. _random = new Random(0x989680);
  31. int liInt = _random.Next(0x989680, 0x5f5e0ff);
  32. return ("+" + liInt + " GS");
  33. }
  34. /// <summary>
  35. /// 从字符串里随机得到,规定个数的字符串.
  36. /// </summary>
  37. /// <param name="codeCount"></param>
  38. /// <param name="allChar"></param>
  39. /// <returns></returns>
  40. public static string GetRandomCode(int codeCount, string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
  41. {
  42. //string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  43. string[] allCharArray = allChar.Split(',');
  44. string randomCode = "";
  45. int temp = -1;
  46. Random rand = new Random();
  47. for (int i = 0; i < codeCount; i++)
  48. {
  49. if (temp != -1)
  50. rand = new Random(temp * i * ((int) DateTime.Now.Ticks));
  51. int t = rand.Next(allCharArray.Length - 1);
  52. while (temp == t)
  53. {
  54. t = rand.Next(allCharArray.Length - 1);
  55. }
  56. temp = t;
  57. randomCode += allCharArray[t];
  58. }
  59. return randomCode;
  60. }
  61. public static string GetNextDataTableName()
  62. {
  63. return ("T" + GetRandom());
  64. }
  65. public static string GetGuid()
  66. {
  67. return Guid.NewGuid().ToString("N");
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. /// <returns></returns>
  73. public static string Uuid()
  74. {
  75. byte[] buffer = Guid.NewGuid().ToByteArray();
  76. long longGuid = BitConverter.ToInt64(buffer, 0);
  77. string value = Math.Abs(longGuid).ToString();
  78. byte[] buf = new byte[value.Length];
  79. int p = 0;
  80. for (int i = 0; i < value.Length;)
  81. {
  82. byte ph = Convert.ToByte(value[i]);
  83. int fix = 1;
  84. if ((i + 1) < value.Length)
  85. {
  86. byte pl = Convert.ToByte(value[i + 1]);
  87. buf[p] = (byte)((ph << 4) + pl);
  88. fix = 2;
  89. }
  90. else
  91. {
  92. buf[p] = ph;
  93. }
  94. if ((i + 3) < value.Length)
  95. {
  96. if (Convert.ToInt16(value.Substring(i, 3)) < 256)
  97. {
  98. buf[p] = Convert.ToByte(value.Substring(i, 3));
  99. fix = 3;
  100. }
  101. }
  102. p++;
  103. i = i + fix;
  104. }
  105. byte[] buf2 = new byte[p];
  106. for (int i = 0; i < p; i++)
  107. {
  108. buf2[i] = buf[i];
  109. }
  110. string cRtn = Convert.ToBase64String(buf2);
  111. cRtn = cRtn.ToLower();
  112. cRtn = cRtn.Replace("/", "");
  113. cRtn = cRtn.Replace("+", "");
  114. cRtn = cRtn.Replace("=", "");
  115. return cRtn.Length == 12 ? cRtn : Uuid();
  116. }
  117. /// <summary>
  118. /// 根据GUID获取16位的唯一字符串
  119. /// </summary>
  120. /// <returns></returns>
  121. public static string GuidTo16String()
  122. {
  123. long i = 1;
  124. foreach (byte b in Guid.NewGuid().ToByteArray())
  125. i *= (b + 1);
  126. return $"{i - DateTime.Now.Ticks:x}";
  127. }
  128. /// <summary>
  129. /// 根据GUID获取19位的唯一数字序列
  130. /// </summary>
  131. /// <returns></returns>
  132. public static long GuidToLongId()
  133. {
  134. byte[] buffer = Guid.NewGuid().ToByteArray();
  135. return BitConverter.ToInt64(buffer, 0);
  136. }
  137. /// <summary>
  138. /// 生成随机数的种子
  139. /// </summary>
  140. /// <returns></returns>
  141. private static int GetNewSeed()
  142. {
  143. byte[] rndBytes = new byte[4];
  144. System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
  145. rng.GetBytes(rndBytes);
  146. return BitConverter.ToInt32(rndBytes, 0);
  147. }
  148. /// <summary>
  149. /// 生成len位随机数
  150. /// </summary>
  151. /// <param name="len"></param>
  152. /// <returns></returns>
  153. public static string GetRandomString(int len)
  154. {
  155. string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
  156. string reValue = string.Empty;
  157. Random rnd = new Random(GetNewSeed());
  158. while (reValue.Length < len)
  159. {
  160. string s1 = s[rnd.Next(0, s.Length)].ToString();
  161. if (reValue.IndexOf(s1, StringComparison.Ordinal) == -1) reValue += s1;
  162. }
  163. return reValue;
  164. }
  165. /// <summary>
  166. /// 获得有序的GUID
  167. /// </summary>
  168. /// <returns></returns>
  169. public static Guid GenerateGuid()
  170. {
  171. byte[] guidArray = Guid.NewGuid().ToByteArray();
  172. var baseDate = new DateTime(1900, 1, 1);
  173. DateTime now = DateTime.Now;
  174. var days = new TimeSpan(now.Ticks - baseDate.Ticks);
  175. TimeSpan msecs = now.TimeOfDay;
  176. byte[] daysArray = BitConverter.GetBytes(days.Days);
  177. byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
  178. Array.Reverse(daysArray);
  179. Array.Reverse(msecsArray);
  180. Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
  181. Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
  182. return new Guid(guidArray);
  183. }
  184. }
  185. }