Skip to content

Commit

Permalink
Shuffle options after checking if length matches, add an explanation …
Browse files Browse the repository at this point in the history
…for throw.
  • Loading branch information
ryouze committed Oct 22, 2024
1 parent 1b6ece2 commit f474094
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/modules/vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ std::vector<Entry> Vocabulary::get_question_options(const Entry &correct_entry,
options.emplace_back(wrong_entry);
}

// Shuffle the options
std::shuffle(options.begin(), options.end(), core::rng::RNG::instance());

// Throw if the number of options is less than the desired number
// This will NOT happen unless the vocabulary in "Vocabulary::Vocabulary()" is modified to have less than 4 entries in a category
if (const std::size_t len = options.size(); len < num_options) {
throw std::runtime_error(fmt::format("Generated '{}' question options, but '{}' were requested", len, num_options));
throw std::runtime_error(fmt::format("Generated '{}' question options, but '{}' were requested; each category in vocabulary needs at least {} entries", len, num_options, num_options));
}

// Shuffle the options
std::shuffle(options.begin(), options.end(), core::rng::RNG::instance());

return options;
}

Expand Down

0 comments on commit f474094

Please sign in to comment.