Background: Making a Flash Card Quiz Game based on QuizMaster but lots customized. My first mostly unsupervised-by-a-tutorial project.
I have some functionality for selecting how many questions you want. Increments of 5 up to 25 max. I’m trying to randomize the questions that actually show in the final quiz. After selecting the categories the user wants to test and the number of questions, they hit “submit” and that will give me a random value which I’ll use at the index to the cleanedQuestionPool to add that question to the finalQuestionPool [the pool you will actually see in the quiz]. There’s 3 special scenarios: The cleanedQuestionPool < numberOfQuestions, cleanedQuestionPool = numberOfQuestions, and cleanQuestionPool > numberOfQuestions.
If <, I want it to just do it’s thing. Duplicates are fine.
If =, I want it to randomize the order, but include all of them.
if >, I want it to only give me whatever number = numberOfQuestions, but no duplicates.
This is my Code:
public void RandomizeQuestionPool()
{
Debug.Log("Randomizing...");
Debug.Log("Number of Questions Selcted is " + MainMenu.numberOfQuestions);
if (cleanedQuestionPool.Count < MainMenu.numberOfQuestions)
{
Debug.Log("Cleaned Question Pool Count is < numberOfQuestions");
for (int i = 0; i < MainMenu.numberOfQuestions; i++)
{
int randomValue = Random.Range(1, cleanedQuestionPool.Count);
finalQuestionPool.Add(cleanedQuestionPool[randomValue]);
}
}
if (cleanedQuestionPool.Count == MainMenu.numberOfQuestions)
{
Debug.Log("Cleaned QPool Count is = numberOfQuestions");
for(int i = 0; i < cleanedQuestionPool.Count; i++)
{
int randomValue = Random.Range(1, cleanedQuestionPool.Count);
QuestionSO questionItem = cleanedQuestionPool[randomValue];
if (finalQuestionPool.Contains(questionItem))
{
Debug.Log("Question Item exists in finalQuestionPool. Continue.");
continue;
}
else
{
Debug.Log("Question Item does not exist in finalQuestionPool. Adding.");
finalQuestionPool.Add(cleanedQuestionPool[randomValue]);
}
}
}
if (cleanedQuestionPool.Count > MainMenu.numberOfQuestions)
{
Debug.Log("Cleaned QPool Count is > numberOfQuestions");
for (int i = 0; i < MainMenu.numberOfQuestions; i++)
{
int randomValue = Random.Range(1, cleanedQuestionPool.Count);
QuestionSO questionItem = cleanedQuestionPool[randomValue];
if (finalQuestionPool.Contains(questionItem))
{
Debug.Log("Question Item exists in finalQuestionPool. Continue.");
continue;
}
else
{
Debug.Log("Question Item does not exist in finalQuestionPool. Adding");
finalQuestionPool.Add(cleanedQuestionPool[randomValue]);
}
}
}
}
I am not quite sure I got it right.
The first one works just fine.
The second one I tested on a list that had 5 questions in the cleanedQuestionPool to start with and after randomizing and adding the results to finalQuestionPool, FQP only had 3 questions. Which I don’t want.
The third option is the same as the second, obviously. Its practically the same code. At first it was working fine with a large number of options, but I tested it with something that was smaller and it’s not.
What am I doing wrong with number 2 & 3?
I’m starting to think it has to do with that “continue”? I want the If statement to continue to loop after checking if the question already exists in our final question pool, until it finds something that doesn’t exist.