I am a beginning programmer and new to c#, I am trying to get a non repeating random string from a list. Once each string in the list has been used once, I want it to start over and use the list again. I am trying to use a HashSet to store the strings once they have been used and then repopulate the original list from the hashtag list. Both Lists and the Random random have been initialized already.
public string GetRandomPrompt()
{
if (_usedList.Count == 0)
{
foreach (string s in _usedList)
{
_prompts.Add(s);
}
_usedList.Clear();
}
string randomPrompt = _prompts[random.Next(_prompts.Count)];
_usedList.Add(randomPrompt);
_prompts.Remove(randomPrompt);
return randomPrompt;
}
The prompts repeat before they have each been used once.
user25072518 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.