Aslah Fida'iyi
Aslah Fida'iyi

Reputation: 41

Random String Generator that does not repeat

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();

Upvotes: 2

Views: 3312

Answers (2)

tinstaafl
tinstaafl

Reputation: 6948

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);
}

Upvotes: 0

Mohit S
Mohit S

Reputation: 14024

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

Upvotes: 1

Related Questions