简体   繁体   中英

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


    }

You're close. But the problem here is that you're selecting randomly from the "semi-sorted" array, while what's really necessary at that point is picking a random permutation. One way to do that is with a Fisher-Yates shuffle .

So combining that with the code you had that worked: (not tested)

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)];
}
int n = stringChars.Length;
while (n > 1) 
{
    int k = random.Next(n--);
    char temp = stringChars[n];
    stringChars[n] = stringChars[k];
    stringChars[k] = temp;
}
string result = new string(stringChars);

Harold's answer is way cleaner, but here's another approach for the whole '100 ways to do the same thing in programming' concept. [Edit: Doh, I reversed the number of digits and letters. Here's a fix:]

public static void Main(string[] args)
    {
        var random = new Random();
        var finalString = string.Empty;
        var finalArray = new string[9];

        for (var i = 0; i < 3; i++)
        {
            var alphabet = random.Next(0, 26);
            var letter = (char) ('a' + alphabet);
            finalArray[i] = letter.ToString().ToUpper();
        }

        for (var i = 3; i < 9; i++)
        {
            var number = random.Next(0, 9);
            finalArray[i] = number.ToString();
        }

        var shuffleArray = finalArray.OrderBy(x => random.Next()).ToArray();
        for (var i = 0; i < finalArray.Length; i++)
        {
            finalString += shuffleArray[i];
        }
        Console.WriteLine(finalString);
        Console.ReadKey();    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM