/// <summary>
/// 数字辅助类
/// xxx
/// </summary>
public class NumHelper
{
#region
/
随机数 //
/
/// <summary>
/// 获取指定长度的随机数|||数字与字母混合
/// </summary>
/// <param name="length">随机数长度</param>
/// <returns>随机数字字母</returns>
public string GetRandom_CN(int length)
{
string all = "0,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,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 = all.Split(',');
string result = "";
Random rand = new Random();
for (int i = 0; i < length; i++)
{
result += allChar[rand.Next(62)];
}
return result;
}
/// <summary>
/// 获取指定长度的随机数|||数字
/// </summary>
/// <param name="length">随机数长度</param>
/// <returns>随机数</returns>
public string GetRandom_N(int length)
{
string all = "0,1,2,3,4,5,6,7,8,9";
string[] allChar = all.Split(',');
string result = "";
Random rand = new Random();
for (int i = 0; i < length; i++)
{
result += allChar[rand.Next(10)];
}
return result;
}
/// <summary>
/// 获取时间+4位随机(共18位唯一)
/// </summary>
/// <returns>18位随机数</returns>
public string GetRandom_T()
{
string result = "";
string four = GetRandom_CN(4);
string dt = DateTime.Now.ToString("yyyyMMddhhmmss");
result = dt + four;
return result;
}
#endregion
#region
/
/// 随机数(彩票) /
/
/// <summary>
/// 获取指定数字范围和指定个数的随机数集合
/// </summary>
/// <param name="indexnum">起始数字</param>
/// <param name="endnum">终止数字</param>
/// <param name="count">数字个数</param>
/// <returns></returns>
public List<int> GetRandomNum(int indexnum, int endnum, int count)
{
if (count > endnum - indexnum) {
return null;//所需数字个数不足
}
List<int> lst = new List<int>();
Random r = new Random();
while (lst.Count < count)
{
int a = 0;
int rannum = r.Next(indexnum, endnum);
for (int i = 0; i < lst.Count; i++)
{
if (lst[i] == rannum) {
a++;
}
}
if (a == 0) {
lst.Add(rannum);
}
}
return BubbleSort(lst);
}
#endregion
#region
/
// 排序 /
/
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="lst">数字集合</param>
/// <returns></returns>
public List<int> BubbleSort(List<int> a)
{
int temp = 0;
for (int i = a.Count - 1; i > 0; --i)
{
for (int j = 0; j < i; ++j)
{
if (a[j + 1] < a[j])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return a;
}
#endregion
}