实测1,000,000 * 100次, 平均每生成1,000,000个随机文件名耗时0.3秒. (测试平台:Dell 640M, T2300, XPSP2)
private static string charDictionary = "0123456789abcdefghijklmnopqrstuvwxyz";
#region GetRandomString
public static string GetRandomString(int length)
{
return GetRandomString(new Random(), length);
}
public static string GetRandomString(string dictionary, int length)
{
return GetRandomString(new Random(), dictionary, length);
}
public static string GetRandomString(Random r, int length)
{
return GetRandomString(r, charDictionary, length);
}
public static string GetRandomString(Random r, string dictionary, int length)
{
if(r == null)
{
r = new Random();
}
StringBuilder sb = new StringBuilder();
char[] chars = dictionary.ToCharArray();
for(int i = 0; i < length; i++)
{
sb.Append(chars[r.Next(0, chars.Length)]);
}
return sb.ToString();
}
#endregion
#region GetRandomFileName
public static string GetRandomFileName()
{
return GetRandomFileName(new Random());
}
public static string GetRandomFileName(Random r)
{
long timeSpan = (long)(DateTime.Now - new DateTime(2000, 1, 1)).TotalMilliseconds;
string fileName = ConvertToRadix(timeSpan, 36);
fileName += GetRandomString(r, 4);
return fileName.ToLower();
}
#endregion
#region ConvertToRadix
public static string ConvertToRadix(long number, byte scale)
{
if(scale < 2 || scale > 36)
{
throw new Exception("Scale number must not be less than 2 and bigger then 36.");
}
char[] dictionary = charDictionary.ToCharArray();
List<char> charList = new List<char>();
long positive = Math.Abs(number);
for(int i = 0; i < 64; i++)
{
if(positive == 0)
{
break;
}
charList.Add(dictionary[positive % scale]);
positive /= scale;
}
charList.Reverse();
return new string(charList.ToArray());
}
#endregion
#region GetRandomString
public static string GetRandomString(int length)
{
return GetRandomString(new Random(), length);
}
public static string GetRandomString(string dictionary, int length)
{
return GetRandomString(new Random(), dictionary, length);
}
public static string GetRandomString(Random r, int length)
{
return GetRandomString(r, charDictionary, length);
}
public static string GetRandomString(Random r, string dictionary, int length)
{
if(r == null)
{
r = new Random();
}
StringBuilder sb = new StringBuilder();
char[] chars = dictionary.ToCharArray();
for(int i = 0; i < length; i++)
{
sb.Append(chars[r.Next(0, chars.Length)]);
}
return sb.ToString();
}
#endregion
#region GetRandomFileName
public static string GetRandomFileName()
{
return GetRandomFileName(new Random());
}
public static string GetRandomFileName(Random r)
{
long timeSpan = (long)(DateTime.Now - new DateTime(2000, 1, 1)).TotalMilliseconds;
string fileName = ConvertToRadix(timeSpan, 36);
fileName += GetRandomString(r, 4);
return fileName.ToLower();
}
#endregion
#region ConvertToRadix
public static string ConvertToRadix(long number, byte scale)
{
if(scale < 2 || scale > 36)
{
throw new Exception("Scale number must not be less than 2 and bigger then 36.");
}
char[] dictionary = charDictionary.ToCharArray();
List<char> charList = new List<char>();
long positive = Math.Abs(number);
for(int i = 0; i < 64; i++)
{
if(positive == 0)
{
break;
}
charList.Add(dictionary[positive % scale]);
positive /= scale;
}
charList.Reverse();
return new string(charList.ToArray());
}
#endregion