123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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");
- }
- /// <summary>
- /// 从字符串里随机得到,规定个数的字符串.
- /// </summary>
- /// <param name="allChar"></param>
- /// <param name="CodeCount"></param>
- /// <returns></returns>
- 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");
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- 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();
- }
- }
- /// <summary>
- /// 根据GUID获取16位的唯一字符串
- /// </summary>
- /// <param name=\"guid\"></param>
- /// <returns></returns>
- 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);
- }
- /// <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="length"></param>
- /// <returns></returns>
- 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;
- }
- /// <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);
- }
- }
- }
|