Select a string consisting of 10 characters from 26 English letters to generate a certain number of unique strings. Comparison methods:
1. Use system. Security. cryptography. rngcryptoserviceprovider to generate a Random Seed and compare it with a general claimed random number.
2. use idictionary <tkey, tvalue> where tkey is the hashcode of the int type to store the string, and tvalue is the string type to store the generated string, the comparison key is used to determine whether the item already exists and to use the ilist <t> storage string for comparison.
3. Use the random truncation string and random string array index to obtain the composition string.
Method for generating the seeds of a random instance:
Static int getrandomseed ()
{
Byte [] bytes = new byte [4];
System. Security. cryptography. rngcryptoserviceprovider RNG = new system. Security. cryptography. rngcryptoserviceprovider ();
RNG. getbytes (bytes );
Return bitconverter. toint32 (bytes, 0 );
}
How to generate a random string:
Static string getrandomstring ()
{
Stringbuilder sbpwd = new stringbuilder ();
Random random = new random (getrandomseed ());
For (INT I = 0; I <length; I ++)
{
Sbpwd. append (strsource. substring (random. Next (0, 25), 1 ));
// Sbpwd. append (sourcearray [random. Next (0, 25)]);
}
Return sbpwd. tostring ();
}
Comparison result:
1. Use the getrandomseed () method to generate the Random Seed and use the character truncation function. Use idictionary <int, string> to generate duplicate items in 20688 Ms. 359 generate items: 1000000
2. The getrandomseed () method is not used to generate the Random Seed and the character truncation is used. idictionary <int, string> takes 1562547 ms to generate duplicate items. 127749442 generated items: 100000
3. use the getrandomseed () method to generate the Random Seed and use the string array idictionary <int, string> to generate duplicate items in 36125 Ms 381 generate items: 1000000 (less efficient using the char array, random acquisition of Char to String Conversion requires packing)
4. Use the getrandomseed () method to generate the Random Seed and use the character truncation. Use ilist <string> to generate duplicate items in 214719 Ms. 2. Generate items: 100000 (more generated items take longer)
It can be seen that the efficiency of using system. Security. cryptography. rngcryptoserviceprovider to generate random seeds is much higher, especially when a large number of random numbers are generated continuously, because the repetition rate of the values generated by random is very low.
Comparing strings with string hashcode is much more efficient than comparing strings directly.
String truncation is more efficient than string array.