2

I've been using this random string generator. But my problem is that this random string generator is repeating itself. I've heard of using the shuffle method but I dont know how to implement it in my code. Any help will be mostly appreciated.

private static Random random = new Random(); public static string RandomString(int length) { const string chars = "0123456789"; return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } public static string RandomString2(int length) { const string chars = "AB"; return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } 

And then in the page load i actually call my 2 methods and combine them in a textbox.

string dummy = RandomString(1); string dummy2 = RandomString2(1); txtTagNumber.Text = dummy2.ToString() + dummy.ToString(); 
5
  • Random and non-repeating are incompatible things. Please clarify what are exact requirements and someone will find suitable duplicate to close this question. Commented Nov 22, 2016 at 3:28
  • What do you mean by "don`t repeat"? Commented Nov 22, 2016 at 3:29
  • How many chars or range does a random string need to have? Please give examples of expected generated strings. Commented Nov 22, 2016 at 3:43
  • @MohitShrivastava The string needs to be in a format like this. E.g: A1, A2, A3, A4, B1, B2, B3. And when I display them on the textbox, there should be no duplicates. So lets say A1 was already used by a user, therefore A1 cannot be shown again by the random. Commented Nov 22, 2016 at 4:03
  • Random won't take care of that, it is a validation concerning to the state of your application ("don't give me something i already have"). So, you have to somehow check if the resulting string was already displayed and call your method again if it was, expecting the new result to be different. Commented Nov 22, 2016 at 4:27

2 Answers 2

1

This might do the trick for you.

Random random = new Random(); const string numchars = "0123456789"; const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; List<string> randStr = new List<string>(); for(int i = 0; i <= 10; i++) { string AlphaRandom = new string(Enumerable.Repeat(chars, 1) .Select(s => s[random.Next(s.Length)]).ToArray()); string NumberRandom = new string(Enumerable.Repeat(numchars, 1) .Select(s => s[random.Next(s.Length)]).ToArray()); if(randStr.Contains(AlphaRandom + NumberRandom)) { i--; } else { randStr.Add(AlphaRandom + NumberRandom); Console.WriteLine(randStr[i]); } } 

Created a random of Random Type then took 2 strings one for alphabets and another for numbers. Created a list of string to add all generated random strings to it. In the loop I am trying to generate 10 random strings. Checking if the list of Random String contains the string which has been generated earlier. if yes then decrement the value of i so that it would still generate 10 strings. and if no matches found than Random string will be added to the list.

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

Another way would be to create a list of all the choices. Then whenever the user makes a choice remove it from the list. Choose random indexes from that list to yield only unique choices:

List<string> choices = new List<string>(); Random rnd = new Random(DateTime.Now.Millisecond); public void PopulateChoices() { choices.Clear(); for(char i = '0'; i < ':';i++) { for(char j = 'A'; j < 'C'; j++) { choices.Add(new string(new char[] { j, i })); } } } public List<string> MakeRandColl(int size) { List<string> randChoices = choices; List<string> retVal = new List<string>(); for(int i = 0; i < size; i++) { string temp = randChoices[rnd.Next(0, randChoices.Count)]; retVal.Add(temp); randChoices.Remove(temp); } return retVal; } public void DeleteChoice(string choice) { choices.Remove(choice); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.