From f474094352fd26a293b0632434fc1efa445a16d8 Mon Sep 17 00:00:00 2001 From: ryouze <98982999+ryouze@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:03:41 +0200 Subject: [PATCH] Shuffle options after checking if length matches, add an explanation for throw. --- src/modules/vocabulary.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/modules/vocabulary.cpp b/src/modules/vocabulary.cpp index 15635fb..bbd41b9 100644 --- a/src/modules/vocabulary.cpp +++ b/src/modules/vocabulary.cpp @@ -128,14 +128,15 @@ std::vector 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; }