using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SysBaseLibs { public sealed class RandomUtil { static Random _random; 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.ToString() + " GS"); } /// /// 从字符串里随机得到,规定个数的字符串. /// /// /// /// public static string GetRandomCode(string allChar, 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[] 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 System.Guid.NewGuid().ToString("N"); } /// /// /// /// public static string UUID() { byte[] buffer = Guid.NewGuid().ToByteArray(); long long_guid = BitConverter.ToInt64(buffer, 0); string _Value = System.Math.Abs(long_guid).ToString(); byte[] buf = new byte[_Value.Length]; int p = 0; for (int i = 0; i < _Value.Length;) { byte ph = System.Convert.ToByte(_Value[i]); int fix = 1; if ((i + 1) < _Value.Length) { byte pl = System.Convert.ToByte(_Value[i + 1]); buf[p] = (byte)((ph << 4) + pl); fix = 2; } else { buf[p] = (byte)(ph); } if ((i + 3) < _Value.Length) { if (System.Convert.ToInt16(_Value.Substring(i, 3)) < 256) { buf[p] = System.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 = System.Convert.ToBase64String(buf2); if (cRtn == null) { cRtn = ""; } cRtn = cRtn.ToLower(); cRtn = cRtn.Replace("/", ""); cRtn = cRtn.Replace("+", ""); cRtn = cRtn.Replace("=", ""); if (cRtn.Length == 12) { return cRtn; } else { return UUID(); } } /// /// 根据GUID获取16位的唯一字符串 /// /// /// public static string GuidTo16String() { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) i *= ((int)b + 1); return string.Format("{0:x}", i - DateTime.Now.Ticks); } /// /// 根据GUID获取19位的唯一数字序列 /// /// public static long GuidToLongID() { byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToInt64(buffer, 0); } /// /// 生成随机数的种子 /// /// 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); } /// /// 生成len位随机数 /// /// /// static public 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) == -1) reValue += s1; } return reValue; } /// /// 获得有序的GUID /// /// 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); } } }