So I'm trying to do a random tip generator that, obviously, shouldn't show the same tip at least until all of them have been seen before
private string getRandomTip()
{
List<string> Titles = new List<string> {
"You can copy the result by clicking over it",
"Remember to press Ctrl + Z if you messed up",
"Check web.com to update the app"};
Random rnd = new Random();
int Index = rnd.Next(0, Titles.Count);
string randomString = Titles[Index];
Titles.RemoveAt(Index);
return randomString;
}
but for some reason this repeats the tips twice or more in a row. I've thought that I could generate random numbers until I get one that isn't the last one but this looks like a bad optimized code.
Could you please help me?
EDIT: Okay guys thank you very much I'm trying with this now
public List<string> Titles = new List<string> {
"You can copy the result by clicking over it",
"This calculator was created by Glow on 21/10/17",
"Click on this tip to show the next one"};
private string getRandomTip()
{
string randomString;
if (Titles.Count > 0) {
Random rnd = new Random();
int Index = rnd.Next(0, Titles.Count);
randomString = Titles[Index];
Titles.RemoveAt(Index);
}
else
{
randomString = "Those were all the tips!";
}
return randomString;
}
and it works perfectly. Thanks again :)