I have a list that is filled with words in order to generate passwords. A number from 1 to 48 is to be appended to these words via the while loop. I then randomly select three elements from this list. The selected elements are saved in an Excel list. The words are saved in the Excel list, but the consecutive number from the while loop is not appended to the word or only the word is displayed in the Excel list. It does not yet work the way I want it to. How do I append a number to a string from a list and call the newly generated string, by random selection, to save it in an Excel list? I ask for your support.
Here my Method with the while-loop:
public string GeneratePassword()
{
int num = 1;
var names = new List<string> { "Ananas","Auto","Affe","Ameise","Apfel","Biene","Baum","Birne","Ball","Blume","Cello","Chips"};
int index = random.Next(names.Count);
while (num < 49)
{
string name = names[index] num;
names.Add(name);
num ;
names.RemoveAt(index);
}
return names[index];
}
Here the section where i call the method:
Program pw = new Program();
var login = new List<string> { "Z00001", "Z00002", "Z00003" };
ws.Cells["B1"].LoadFromArrays(new List<string[]>(new[] { login.ToArray() }));
var password = new List<string> { pw.GeneratePassword(), pw.GeneratePassword(),
pw.GeneratePassword() };
ws.Cells["B2"].LoadFromArrays(new List<string[]>(new[] { password.ToArray() }));
CodePudding user response:
One thing that's sometimes helpful is to break the problem down into discreet parts, then write a method that performs just a single thing for each part. In this case, we want to generate a list of names, add random numbers to the end of each name, and then pick a random item from the list (but don't pick the same item more than once).
One way to pick a random item from a list until all items are picked is to sort the list randomly, then take from the list sequentially (removing the item as you take it).
Below are some methods that I think will help. One that gets the names, one that randomly sorts the list, one that adds a random number to each item in a list, and then the main one (GetPassword
), which returns and removes the first item from the list. It also generates the list if needed.
private static Random rnd = new Random();
private static List<string> passwords = new List<string>();
// Returns a list of names
public static List<string> GetNames()
{
return new List<string> {
"Ananas","Auto","Affe","Ameise","Apfel","Biene","Baum",
"Birne","Ball","Blume","Cello","Chips"};
}
// Sorts a list in random order
public static List<string> RandomSort(List<string> items)
{
return items.OrderBy(item => rnd.Next()).ToList();
}
// Returns a list of the input items with a random
// number added to the end of each item
public static List<string> AppendNumberToItems(List<string> input,
int min = 1, int max = 48)
{
var newItems = new List<string>();
foreach(var item in input) newItems.Add(item rnd.Next(min, max 1));
return newItems;
}
// Returns the next password in our password list
public static string GetPassword()
{
// In case we're out of passwords, generate some new ones
if (passwords.Count == 0) passwords =
AppendNumberToItems(RandomSort(GetNames()), 1, 48);
// Take the first password from the list
string password = passwords[0];
// Then remove that password from the list
passwords = passwords.Skip(1).ToList();
// And return it
return password;
}