同事Rex写的一个随机字符串函数,使用Guid.GetHashCode作种子,感觉不错,与大家分享一下:
using System;
namespace Newegg.Applications.IAPS.Common
{
/// <summary>
/// Summary description for RandomHelper.
/// </summary>
public class RandomHelper
{
private RandomHelper()
{
}
private const string m_DefaultAlphaChars = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGJKMPQRSTUVWXYZ";
public static string GetRandomString(int length)
{
if (length <= 0)
{
length = 4;
}
string code = string.Empty;
int idx;
int seed = Guid.NewGuid().GetHashCode();
Random rdm = new Random(seed);
string alphaCharList = m_DefaultAlphaChars;
for (int i = 0; i < length; i++)
{
idx = rdm.Next(alphaCharList.Length - 1);
code += alphaCharList[idx];
}
return code;
}
}
}
namespace Newegg.Applications.IAPS.Common
{
/// <summary>
/// Summary description for RandomHelper.
/// </summary>
public class RandomHelper
{
private RandomHelper()
{
}
private const string m_DefaultAlphaChars = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGJKMPQRSTUVWXYZ";
public static string GetRandomString(int length)
{
if (length <= 0)
{
length = 4;
}
string code = string.Empty;
int idx;
int seed = Guid.NewGuid().GetHashCode();
Random rdm = new Random(seed);
string alphaCharList = m_DefaultAlphaChars;
for (int i = 0; i < length; i++)
{
idx = rdm.Next(alphaCharList.Length - 1);
code += alphaCharList[idx];
}
return code;
}
}
}