C # Unique order number generation code

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {            stringUniquenum =Generateordernumber (); Console.WriteLine ("Unique code: {0}\t length: {1}", Uniquenum, uniquenum.length); //tests whether duplicates are generatedConsole.WriteLine ("time +rngcryptoserviceprovider () combines the generated unique values as follows:"); string_tempnum =string.            Empty;  for(inti =0; I < +; i++)            {                stringUNum =Generateordernumber ();                Console.WriteLine (UNum); if(string. Equals (UNum, _tempnum)) {Console.WriteLine ("duplicate on value exists, press ENTER to continue");                Console.readkey (); }                //The current thread of sleep is for inertia, and thus does not produce duplicate values. You can comment it out and test it .Thread.Sleep ( -); _tempnum=UNum; }        }        /// <summary>        ///Unique order number generation/// </summary>        /// <returns></returns>         Public Static stringGenerateordernumber () {stringStrdatetimenumber = DateTime.Now.ToString ("yyyymmddhhmmssms"); stringStrrandomresult = Nextrandom ( +,1).            ToString (); returnStrdatetimenumber +Strrandomresult; }        /// <summary>        ///Reference: RNGCryptoServiceProvider examples on MSDN/// </summary>        /// <param name= "numseeds" ></param>        /// <param name= "Length" ></param>        /// <returns></returns>        Private Static intNextrandom (intNumseeds,intlength) {            //Create A byte array to hold the random value.             byte[] Randomnumber =New byte[length]; //Create A new instance of the RNGCryptoServiceProvider. System.Security.Cryptography.RNGCryptoServiceProvider rng =NewSystem.Security.Cryptography.RNGCryptoServiceProvider (); //Fill the array with a random value. rng.            GetBytes (Randomnumber); //Convert the byte to a uint value to make the modulus operation easier.             UINTRandomresult =0x0;  for(inti =0; i < length; i++) {Randomresult|= ((UINT) Randomnumber[i] << (Length-1-i) *8)); }            return(int) (randomresult% numseeds) +1; }    }}
View Code

C # Unique order number generation code

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.