# Checking list every time for duplicates

Hello. I already checked in the forum but couldn’t find something similar to my problem… I generate 2 random numbers every time I click a button and I am trying to save those 2 numbers in a list so I can check every time I click the button for duplicates.
For example, on the first run I get ‘5’ and ‘8’, after x times I get again this combination ‘5’ and ‘8’ so I want to prevent this.

``````questionNumbersList.AddRange(new float[] { firstNumber, secondNumber });
for (int i = 0; i < questionNumbersList.Count; i++)
{
Debug.Log("Position " + i + " with list number: " + questionNumbersList*);*
``````

}
I am not sure how I can check my list for both numbers and see if I already got ‘5’, ‘8’ skip it and generate two new numbers.

if you want two floats, Use Vector2. A Vector2 variable is just two floats.

``````		public List<Vector2> used = new List<Vector2>();

public Vector2 UniqueRandom(){
Vector2 r = new Vector2 (0,0);

while(true){
r = new Vector2(Random.Range(0,10),Random.Range(0,10));
if(!used.Contains(r)){break;}
if(used.Count>99){print("Ooops. all Number combos used");break;}}
return r;
}

void Start () {

//these float combos are never the same.
print(UniqueRandom());
print(UniqueRandom());
print(UniqueRandom());
print(UniqueRandom());
print(UniqueRandom());
print(UniqueRandom());
}
``````

im using a while loop to continue picking as long as there is match in the list.
careful of while loops though. If for whatever reason the loop is not stopped,
it becomes an infinite loop and really really bad things happen. LOL Thats why i put the extra line to break the loop if you run out of possabilitys!

You could store them in a HashSet instead of a list (and of course as Vector2). That’s exactly what they are for.