Random string without duplicates

I am trying to create a random string of 16 characters with NO DUPLICATES. I want to say that it should not be difficult, but I am stuck.

I use 2 methods, one to generate a key and the other to remove duplicate characters. I basically created a while loop to make sure that the generated string is 16 characters long.

Something is wrong in my logic, because it just shoots at a 16-char string with duplicates. It just won't work.

Code:

public string RemoveDuplicates(string s) { string newString = string.Empty; List<char> found = new List<char>(); foreach (char c in s) { if (found.Contains(c)) continue; newString += c.ToString(); found.Add(c); } return newString; } public static string GetUniqueKey(int maxSize) { char[] chars = new char[62]; chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray(); byte[] data = new byte[1]; RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider(); crypto.GetNonZeroBytes(data); data = new byte[maxSize]; crypto.GetNonZeroBytes(data); StringBuilder result = new StringBuilder(maxSize); foreach (byte b in data) { result.Append(chars[b % (chars.Length)]); } return result.ToString(); } string builder = ""; do { builder = GetUniqueKey(16); RemoveDuplicates(builder); lblDir.Text = builder; Application.DoEvents(); } while (builder.Length != 16); 
+4
source share
5 answers

Consider using a shuffle algorithm with which you will mix a string with unique characters, and then just pick up the first 16 characters.

You can do this in place by highlighting one StringBuffer that will contain your source data ("abc ...."), and simply use the version of the Durstenfeld algorithm to change your buffer than returning the first 16 characters.

+8
source

There are many algorithms for this.

One simple:

  • Fill the character array with available characters.
  • Shuffle the array.
  • Take the first N elements (where N is the number of characters you need).

Code example:

 using System; namespace ConsoleApplication2 { internal class Program { private static void Main(string[] args) { var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray(); Random rng = new Random(); for (int i = 0; i < 10; ++i) { string randomString = RandomString(16, chars, rng); Console.WriteLine(randomString); } } public static string RandomString(int n, char[] chars, Random rng) { Shuffle(chars, rng); return new string(chars, 0, n); } public static void Shuffle(char[] array, Random rng) { for (int n = array.Length; n > 1; ) { int k = rng.Next(n); --n; char temp = array[n]; array[n] = array[k]; array[k] = temp; } } } } 
+7
source
 const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; var r = new Random(); var s = new string(chars.OrderBy(x => r.Next()).Take(16).ToArray()); 
+5
source

I use the GUID generation method, which it itself generates random strings, and you can change it if the number appears at the beginning, use the code below:

 string guid = System.Guid.NewGuid().ToString("N"); while (char.IsDigit(guid[0])) guid = System.Guid.NewGuid().ToString("N"); 

Hope this helps.

0
source

See if this helps:

  RandomString() { string randomStr = Guid.NewGuid().ToString(); randomStr = randomStr.Replace("-", "").Substring(0, 16); Console.WriteLine(randomStr); } 

This returns an alphanumeric string.

0
source

All Articles