asp.net random code generation base class (random letters, random numbers, random letters + numbers) _ Practical Tips

Source: Internet
Author: User
Tags datetime
Copy Code code as follows:

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>
Random function
</summary>
public class Randomobject
{
#region Digital Random number
<summary>
Digital random number
</summary>
<param name= "n" > Build length </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 number and letter random numbers
<summary>
Number and letter random numbers
</summary>
<param name= "n" > Build length </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
Random number of #region letters
<summary>
Random Number of letters
</summary>
<param name= "n" > Build length </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 Date Random function
<summary>
Date Random function
</summary>
<param name= "RA" > Length </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 Build GUID
<summary>
Generate GUID
</summary>
<returns></returns>
public static string GetGuid ()
{
System.Guid g = System.Guid.NewGuid ();
return g.tostring ();
}
#endregion
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.