2021年3月18日星期四

How to get rid of duplicates from random name generator?

I'm trying to make a random generator that selects a specific number of names from a list and pairs them together.

private void button1_Click(object sender, EventArgs e)          {              string[] lines = File.ReadAllLines(@"C:\Users\Brown\source\repos\Generator\bin\names.txt");                List<string> source = new List<string>();              int n = int.Parse(textBox1.Text);              for (int i = 0; i < n; i++)              {                  source.Add(lines[new Random().Next(lines.Length)]);              }              string joinNames = string.Join(" x ", source);                genResult.Text = joinNames;          }  

I got it to work but when i click the generate button I see duplicates of some names. I'm very new to C# so any help would be appreciated.

https://stackoverflow.com/questions/66701517/how-to-get-rid-of-duplicates-from-random-name-generator March 19, 2021 at 09:49AM

没有评论:

发表评论