Implementation Code of random numbers returned by ASP. NET

Source: Internet
Author: User

CopyCode The Code is as follows: // <summary>
/// Return a random number
/// </Summary>
/// <Param name = "vcodenum"> </param>
/// <Returns> </returns>
Public String rndnum (INT vcodenum)
{
String vchar = ";
String [] vcarray = vchar. Split (',');
String vnum = ""; // stringbuilder is not required because the string is short.
Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.
// Use a simple Algorithm To ensure different random numbers are generated
Random Rand = new random ();
For (INT I = 1; I <vcodenum + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}
// Int T = Rand. Next (35 );
Int T = Rand. Next (9 );
If (temp! =-1 & temp = T)
{
Return rndnum (vcodenum );
}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum;
}

only string code = rndnum (4) is required for the call.
a four-digit random number is obtained.
If a random letter is required,
string vchar = ", a, B"; Add the letter.
modify int T = Rand at the same time. in next (9);, 9 is changed to the length of vchar but can.

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.