0
string[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
                  "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
string current = "";
string[] newarr = new string [26];

Random rand = new Random();

for (int a = 0; a < 26 /*How many passwords*/; a++)
{
    while (current.Length < 5 /*Length of passwords*/)
    {
        current = current + alpha[rand.Next(26)];
    }

    newarr[a] = current;
    Console.WriteLine(newarr[a]);
}

The for loop is used to execute the code many times, thus providing many random strings, and the while loop is used to add a random digit to the password.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
benranger
  • 43
  • 3
  • Read up on how to seed `Random`. – Scott Hunter Aug 10 '20 at 16:48
  • 1
    This is a **great** opportunity to learn how to debug your code using the **[free, awesome, built-in Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Aug 10 '20 at 16:49
  • 1
    You're seeding your random correctly, the problem is you fill up current once and it's length is always >5 after that. Place a current=string.Empty; after you write to console in the loop and you'll get the desired effect. – Mikael Aug 10 '20 at 16:55
  • 1
    You never reset "current" variable, and only assign to it in the first iteration of the for loop. , and then for all iterations you assign it to newarr. – dugas Aug 10 '20 at 16:55
  • Came back to add a dotnetfiddle since the question was previously closed, but I guess it got reopened after I answered via comment. Anyway, here's a working example: https://dotnetfiddle.net/GnekWC – Mikael Aug 10 '20 at 17:07

1 Answers1

4

The problem here is that you're only setting current one time (until it's 5 characters long), and then continually adding it to an array.

If you add current = string.Empty after adding it to the newarr array (basically resetting current so it can be assigned a new value), you'll get the expected results.

Better yet, we should just declare current inside the loop, since we don't need it elsewhere. This way it gets reassigned at the beginning of each iteration.

As a side note, it's usually good to use the Length property of the array you're looping over as the bounds for the loop. This way, if someone makes the array smaller, you don't have to update all the loop values:

string[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                    "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

string[] passwords = new string[alpha.Length];

Random rand = new Random();

for (int a = 0; a < passwords.Length; a++)
{
    var password = "";

    while (password.Length < 5)
    {
        password = password + alpha[rand.Next(alpha.Length)];
    }

    passwords[a] = password;
    Console.WriteLine(passwords[a]);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • +1, Also suggest declaring variable "current" inside of for loop, before while loop, which will also remove need to set to empty string at end of loop body. – dugas Aug 10 '20 at 17:00
  • 1
    @dugas great suggestion; added it – Rufus L Aug 10 '20 at 17:04