public List<string> myListOfStrings; // Declaring Lists of strings
public List<string> excludeThisWords; // Declaring Lists of string to exclude
public List<string> newListOfRandomStrings;
public int numberOfRandomPicks = 4;
// Start is called before the first frame update
void Start()
{
myListOfStrings.Add("String_1");
myListOfStrings.Add("String_2");
myListOfStrings.Add("String_3");
myListOfStrings.Add("String_4");
myListOfStrings.Add("String_5");
myListOfStrings.Add("String_6");
myListOfStrings.Add("String_7");
myListOfStrings.Add("String_8");
myListOfStrings.Add("String_9");
myListOfStrings.Add("String_10");
excludeThisWords.Add("String_7");
excludeThisWords.Add("String_1");
PickRandomStringsButExcludeSome();
}
public void PickRandomStringsButExcludeSome()
{
int indexOfRandomWord;
string wordFromList;
// Getting 4 Random Words from the List -> numberOfRandomPicks = 4;
for (int i = 0; i < numberOfRandomPicks; i++)
{
indexOfRandomWord = Random.Range(0, myListOfStrings.Count);
wordFromList = myListOfStrings[indexOfRandomWord];
for (int j = 0; j < excludeThisWords.Count; j++)
{
if (wordFromList == excludeThisWords[j])
{
while (wordFromList == excludeThisWords[j])
{
indexOfRandomWord = Random.Range(0, myListOfStrings.Count);
wordFromList = myListOfStrings[indexOfRandomWord];
}
}
}
//Debug.Log(wordFromList);
newListOfRandomStrings.Add(wordFromList);
excludeThisWords.Add(wordFromList);
//Debug.Log(excludeThisWords);
}
}
Related