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