Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)
程序员文章站
2022-04-17 13:27:48
复制代码 代码如下:using system; using system.data; using system.configuration; using system.we...
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.text;
namespace ec
{
/// <summary>
/// 随机函数
/// </summary>
public class randomobject
{
#region 数字随机数
/// <summary>
/// 数字随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string randnum(int n)
{
char[] arrchar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
stringbuilder num = new stringbuilder();
random rnd = new random(datetime.now.millisecond);
for (int i = 0; i < n; i++)
{
num.append(arrchar[rnd.next(0, 9)].tostring());
}
return num.tostring();
}
#endregion
#region 数字和字母随机数
/// <summary>
/// 数字和字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string randcode(int n)
{
char[] arrchar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','q','p','r','t','s','v','u','w','x','y','z'
};
stringbuilder num = new stringbuilder();
random rnd = new random(datetime.now.millisecond);
for (int i = 0; i < n; i++)
{
num.append(arrchar[rnd.next(0, arrchar.length)].tostring());
}
return num.tostring();
}
#endregion
#region 字母随机数
/// <summary>
/// 字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string randletter(int n)
{
char[] arrchar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'_',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','q','p','r','t','s','v','u','w','x','y','z'
};
stringbuilder num = new stringbuilder();
random rnd = new random(datetime.now.millisecond);
for (int i = 0; i < n; i++)
{
num.append(arrchar[rnd.next(0, arrchar.length)].tostring());
}
return num.tostring();
}
#endregion
#region 日期随机函数
/// <summary>
/// 日期随机函数
/// </summary>
/// <param name="ra">长度</param>
/// <returns></returns>
public static string daterndname(random ra)
{
datetime d = datetime.now;
string s = null, y, m, dd, h, mm, ss;
y = d.year.tostring();
m = d.month.tostring();
if (m.length < 2) m = "0" + m;
dd = d.day.tostring();
if (dd.length < 2) dd = "0" + dd;
h = d.hour.tostring();
if (h.length < 2) h = "0" + h;
mm = d.minute.tostring();
if (mm.length < 2) mm = "0" + mm;
ss = d.second.tostring();
if (ss.length < 2) ss = "0" + ss;
s += y + m + dd + h + mm + ss;
s += ra.next(100, 999).tostring();
return s;
}
#endregion
#region 生成guid
/// <summary>
/// 生成guid
/// </summary>
/// <returns></returns>
public static string getguid()
{
system.guid g = system.guid.newguid();
return g.tostring();
}
#endregion
}
}
下一篇: 孔夫子搬家歇后语及解释