| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System;
- namespace CommonTool
- {
- public class UtilRandom
- {
- static Random _random;
- public static int GetRandomSeed()
- {
- byte[] bytes = new byte[4];
- System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
- rng.GetBytes(bytes);
- return BitConverter.ToInt32(bytes, 0);
- }
- public static int GetIntRandom()
- {
- if (_random == null)
- _random = new Random(0x989680);
- return _random.Next(0x989680, 0x5f5e0ff);
- }
- public static string GetRandom()
- {
- if (_random == null)
- _random = new Random(0x989680);
- int liInt = _random.Next(0x989680, 0x5f5e0ff);
- return ("R" + liInt.ToString());
- }
- public static string GetRandom1()
- {
- if (_random == null)
- _random = new Random(0x989680);
- int liInt = _random.Next(0x989680, 0x5f5e0ff);
- return ("+" + liInt + " GS");
- }
- /// <summary>
- /// 从字符串里随机得到,规定个数的字符串.
- /// </summary>
- /// <param name="codeCount"></param>
- /// <param name="allChar"></param>
- /// <returns></returns>
- 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")
- {
- //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";
- string[] allCharArray = allChar.Split(',');
- string randomCode = "";
- int temp = -1;
- Random rand = new Random();
- for (int i = 0; i < codeCount; i++)
- {
- if (temp != -1)
- rand = new Random(temp * i * ((int) DateTime.Now.Ticks));
- int t = rand.Next(allCharArray.Length - 1);
- while (temp == t)
- {
- t = rand.Next(allCharArray.Length - 1);
- }
- temp = t;
- randomCode += allCharArray[t];
- }
- return randomCode;
- }
- public static string GetNextDataTableName()
- {
- return ("T" + GetRandom());
- }
- public static string GetGuid()
- {
- return Guid.NewGuid().ToString("N");
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public static string Uuid()
- {
- byte[] buffer = Guid.NewGuid().ToByteArray();
- long longGuid = BitConverter.ToInt64(buffer, 0);
- string value = Math.Abs(longGuid).ToString();
- byte[] buf = new byte[value.Length];
- int p = 0;
- for (int i = 0; i < value.Length;)
- {
- byte ph = Convert.ToByte(value[i]);
- int fix = 1;
- if ((i + 1) < value.Length)
- {
- byte pl = Convert.ToByte(value[i + 1]);
- buf[p] = (byte)((ph << 4) + pl);
- fix = 2;
- }
- else
- {
- buf[p] = ph;
- }
- if ((i + 3) < value.Length)
- {
- if (Convert.ToInt16(value.Substring(i, 3)) < 256)
- {
- buf[p] = Convert.ToByte(value.Substring(i, 3));
- fix = 3;
- }
- }
- p++;
- i = i + fix;
- }
- byte[] buf2 = new byte[p];
- for (int i = 0; i < p; i++)
- {
- buf2[i] = buf[i];
- }
- string cRtn = Convert.ToBase64String(buf2);
- cRtn = cRtn.ToLower();
- cRtn = cRtn.Replace("/", "");
- cRtn = cRtn.Replace("+", "");
- cRtn = cRtn.Replace("=", "");
- return cRtn.Length == 12 ? cRtn : Uuid();
- }
- /// <summary>
- /// 根据GUID获取16位的唯一字符串
- /// </summary>
- /// <returns></returns>
- public static string GuidTo16String()
- {
- long i = 1;
- foreach (byte b in Guid.NewGuid().ToByteArray())
- i *= (b + 1);
- return $"{i - DateTime.Now.Ticks:x}";
- }
- /// <summary>
- /// 根据GUID获取19位的唯一数字序列
- /// </summary>
- /// <returns></returns>
- public static long GuidToLongId()
- {
- byte[] buffer = Guid.NewGuid().ToByteArray();
- return BitConverter.ToInt64(buffer, 0);
- }
- /// <summary>
- /// 生成随机数的种子
- /// </summary>
- /// <returns></returns>
- private static int GetNewSeed()
- {
- byte[] rndBytes = new byte[4];
- System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
- rng.GetBytes(rndBytes);
- return BitConverter.ToInt32(rndBytes, 0);
- }
- /// <summary>
- /// 生成len位随机数
- /// </summary>
- /// <param name="len"></param>
- /// <returns></returns>
- public static string GetRandomString(int len)
- {
- string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
- string reValue = string.Empty;
- Random rnd = new Random(GetNewSeed());
- while (reValue.Length < len)
- {
- string s1 = s[rnd.Next(0, s.Length)].ToString();
- if (reValue.IndexOf(s1, StringComparison.Ordinal) == -1) reValue += s1;
- }
- return reValue;
- }
- /// <summary>
- /// 获得有序的GUID
- /// </summary>
- /// <returns></returns>
- public static Guid GenerateGuid()
- {
- byte[] guidArray = Guid.NewGuid().ToByteArray();
- var baseDate = new DateTime(1900, 1, 1);
- DateTime now = DateTime.Now;
- var days = new TimeSpan(now.Ticks - baseDate.Ticks);
- TimeSpan msecs = now.TimeOfDay;
- byte[] daysArray = BitConverter.GetBytes(days.Days);
- byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
- Array.Reverse(daysArray);
- Array.Reverse(msecsArray);
- Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
- Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
- return new Guid(guidArray);
- }
-
- }
- }
|