C # generates no duplicate verification code

Source: Internet
Author: User
Tags datetime rand repetition

Recently the company project made a vouchers volume of amateur, need to generate random 8-digit, letter combination of barcode. Now posted 2 kinds of program source code for everyone to learn and reference.

Method 1.

private static char[] constant = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', S ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};
public string Pxkt_getcharfont (int strlength)
{
System.Text.StringBuilder newrandom = new System.Text.StringBuilder (62);
Random rd = new Random (Guid.NewGuid (). GetHashCode ())//Ensure that the generated random characters never repeat

Random rd= new Random (); Can not write this, a small number of 60 is not a problem, many will have a lot of repetition
for (int i = 0; i < strlength; i++)
{
Newrandom.append (constant[rd. Next (62)]);
}
return newrandom.tostring ();
}

Method 2

public string Randomnum (int n)//
{
Defines a string that includes numbers, uppercase letters, and lowercase English letters
String Strchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l , m,n,o,p,q,r,s,t,u,v,w,x,y,z ";
Converting Strchar strings to arrays
The String.Split method returns a string array that contains the substrings in this instance delimited by the elements of the specified char array.
string[] Vcarray = Strchar. Split (', ');
String vnum = "";
Record the last random number and try to avoid a few of the same random numbers
int temp =-1;
A simple algorithm is used to guarantee the difference of generating random numbers
Random rd = new Random (Guid.NewGuid (). GetHashCode ())//Ensure that the generated random characters never repeat

Random rd= new Random (); Can not write this, a small number of 60 is not a problem, many will have a lot of repetition

for (int i = 1; i < n + 1; i++)
{
if (temp!=-1)
{
The unchecked keyword is used to cancel overflow checking of integer arithmetic operations and conversions.
The Datetime.ticks property gets the number of ticks that represent the date and time of this instance.
Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks));
}
The Random.next method returns a nonnegative random number that is less than the specified maximum value.
int t = rand. Next (61);
if (temp!=-1 && temp = t)
{
return Randomnum (n);
}
temp = t;
Vnum + = vcarray[t];
}
Return vnum;//returns the generated random number
}

}

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.