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 Byecity2011.WebSite. Utils
{
/// <Summary>
/// Generate a random code class
/// </Summary>
Public class RandomObj
{
/// <Summary>
///
/// </Summary>
Public RandomObj ()
{
}
# Region digital Random Number
/// <Summary>
/// Random Number
/// </Summary>
/// <Param name = "n"> generation 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 random numbers and letters
/// <Summary>
/// Random numbers and letters
/// </Summary>
/// <Param name = "n"> generation length </param>
/// <Returns> </returns>
Public static string RandCode (int n)
{
Char [] arrChar = new char [] {
'A', 'B', 'D', 'C', 'E', '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', '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 Random Number
/// <Summary>
/// Random number of letters
/// </Summary>
/// <Param name = "n"> generation length </param>
/// <Returns> </returns>
Public static string RandLetter (int n)
{
Char [] arrChar = new char [] {
'A', 'B', 'D', 'C', 'E', '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', '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 generation GUID
/// <Summary>
/// Generate GUID
/// </Summary>
/// <Returns> </returns>
Public static string GetGuid ()
{
System. Guid g = System. Guid. NewGuid ();
Return g. ToString ();
}
# Endregion
# Region: generate a random password. This method is called when the password is sent.
/// <Summary>
/// Generate a random password. This method is called when the password is sent.
/// </Summary>
/// <Param name = "pwdchars"> specify which characters can be used for the generated random password string </param>
/// <Param name = "pwdlen"> specify the length of the generated random password string </param>
/// <Returns> </returns>
Public static string MakePassword (string pwdchars, int pwdlen)
{
String tmpstr = "";
Int iRandNum;
Random rnd = new Random ();
For (int I = 0; I <pwdlen; I ++)
{
IRandNum = rnd. Next (pwdchars. Length );
Tmpstr + = pwdchars [iRandNum];
}
Return tmpstr;
}
# Endregion
}
}