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");
}
///
/// 从字符串里随机得到,规定个数的字符串.
///
///
///
///
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");
}
///
///
///
///
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();
}
///
/// 根据GUID获取16位的唯一字符串
///
///
public static string GuidTo16String()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
i *= (b + 1);
return $"{i - DateTime.Now.Ticks:x}";
}
///
/// 根据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位随机数
///
///
///
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;
}
///
/// 获得有序的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);
}
}
}