Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)

这篇文章主要介绍了Asp NET 随机码生成基类(随机字母,随机数字,随机字母+数字),在开发过程应该对大家很有帮助,小编结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

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
}
}

以上就是Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)。成败一笑过,潇洒向前行。人生充满变数,不知下一分下一秒我们将在何处。所以,别让自己的心太累,简单一点,快乐一些,看花开花落,听流水潺潺,好好珍惜生活的每一天,轻松快乐淡定从容地去生活!更多关于Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

asp.net中MVC的处理流程详解

ASP.NET Core的日志系统介绍