asp.net return random Number implementation code _ Practical skills

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

<summary>
return random number
</summary>
<param name= "Vcodenum" ></param>
<returns></returns>
public string rndnum (int vcodenum)
{
String Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] Vcarray = Vchar.split (', ');
String vnum = "";//because the strings are very short, you don't have to StringBuilder.
int temp = -1;//record last random value, try to avoid producing several random numbers
A simple algorithm is used to guarantee the difference of generating random numbers
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 invocation;
This results in 4-bit random numbers.
If you need a random number of letters,
String Vchar = "0,1,2,3,4,5,6,7,8,9,a,b"; Add the letters.
also modify int t = rand. Next (9); The 9 in the Vchar is changed to the length but can be.

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.