mardi 26 avril 2016

How do I generate a random alphanumeric array that has 3 letter and 6 digits in c#?

I am trying to generate a random alphanumeric array that consist of 3 letters and 6 digits. The entire array must be random. The only way I could think of is generating 2 individual random arrays and then merging them and randomizing the merged array. Any help would be appreciated. I specifically need help on ensuring that the correct number of variable types are stored. Here is my semi-working code:

 static void Main(string[] args)
    {
        var alphabetic = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var numeric = "0123456789";
        var stringChars = new char[9];
        var random = new Random();


        for (int i = 0; i < 3; i++)
        {
            stringChars[i] = alphabetic[random.Next(alphabetic.Length)];
        }
        for(int i = 3; i< stringChars.Length; i++)
        {
            stringChars[i] = numeric[random.Next(numeric.Length)];
        }


        var ranChars = new char[9];
        var semisorted = new String(stringChars);

        for (int i=0; i< ranChars.Length; i++)
        {
            ranChars[i] = semisorted[random.Next(semisorted.Length)];
        }


        var final = new string(ranChars);

        Console.WriteLine("{0}", final);
        Console.ReadLine();


    }




Aucun commentaire:

Enregistrer un commentaire