RandomUtil.cs 7.3 KB

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