Wiris

Documentation / Nubric

  • Demos
  • Visit our website
  • Contact us
  • MathType

    • WirisQuizzes

      • Nubric

        • CalcMe

          • MathPlayer

            • Store FAQ

              • MathFlow

                • BF FAQ

                  • Miscellaneous

                    • Wiris Integrations

                      • Home
                      • Nubric
                      • Using Nubric
                      • Algorithm Mode in Nubric

                      Patterns and best practices for Algorithm Mode in Nubric

                      Reading time: 2min

                      Algorithm Mode in Nubric allows you to apply advanced logic to dynamic mathematical questions. However, poorly structured algorithms can make questions difficult to maintain, debug, or extend. This article explains common patterns and best practices for writing clearer, more reliable, and reusable algorithms in Algorithm Mode.

                      What It Is

                      When writing algorithms in Algorithm Mode, the same types of logic appear repeatedly across different exercises:

                      • Generating valid random values.
                      • Enforcing mathematical constraints.
                      • Guaranteeing specific properties of solutions.
                      • Avoiding invalid edge cases.

                      These are examples of advanced logic: the rules and calculations you define to control question generation. Reusable patterns help you structure this logic so that your algorithms are easier to write and maintain.

                      The patterns below are recommended approaches rather than mandatory rules.

                      Why It Matters

                      Without consistent patterns, question algorithms can become:

                      • Difficult to debug.
                      • Unreliable when random values change.
                      • Hard to understand when revisited later.
                      • Difficult for colleagues to reuse.

                      Applying common patterns in Algorithm Mode helps ensure that:

                      • Algorithms remain readable.
                      • Constraints are explicitly enforced.
                      • Question generation produces valid variations.
                      • Logic can be reused across different exercises.

                      This becomes especially important when creating large collections of dynamic questions.

                      How It Works

                      The following practices help you organize algorithms in Algorithm Mode. They describe ways to structure your logic rather than execution stages automatically enforced by Nubric.

                      Stage 1: Generate base values first

                      Start by generating the random values on which the rest of the algorithm depends. For example:

                      a = random(1,9)
                      b = random(2,9)

                      These variables provide the starting values for subsequent constraints and calculations.

                      Stage 2: Enforce constraints with loops

                      When generated values must satisfy mathematical properties, use conditions and loops to check them and regenerate values when necessary. Examples include:

                      • Ensuring numbers are coprime.
                      • Avoiding repeated denominators.
                      • Requiring perfect square discriminants.

                      The following pseudocode illustrates the general structure:

                      repeat
                          generate values
                      until condition_is_satisfied

                      Alternatively:

                      while condition_not_met
                          regenerate values
                      end

                      These examples illustrate the logic rather than complete code to paste into Algorithm Mode. Ensure that the condition can be satisfied and that the loop can terminate.

                      Stage 3: Compute solutions after validating the values

                      Once the generated values satisfy your constraints, compute the solution variables. For a fraction addition question with previously defined numerators and non-zero denominators:

                      solution = a/b + c/d

                      Keeping solution calculations separate from value generation and validation improves readability and reduces errors.

                      Stage 4: Control edge cases early

                      Exclude problematic values when defining the initial variables whenever possible. Common approaches include:

                      • Excluding zero when it would produce an invalid calculation.
                      • Avoiding repeated denominators when the exercise requires different ones.
                      • Restricting values to an appropriate range.

                      For example, the following expression excludes zero from the range:

                      random([-10..10]/[0])

                      Excluding unwanted values at generation time reduces the need to handle them later in the algorithm.

                      Stage 5: Use helper functions when logic repeats

                      If the same calculation or generation rule appears multiple times, define a small function in Algorithm Mode. For example:

                      r() := random([-10..10]/[0])

                      This function expresses a reusable rule for generating a non-zero value. Using helper functions improves readability and makes the rule easier to update.

                      Key Rules or Behaviours

                      • Define the required variables before computing values that depend on them.
                      • Enforce mathematical constraints explicitly in your algorithm.
                      • Keep solution calculations separate from generation and validation logic.
                      • Ensure that loops can terminate and avoid unnecessary nested loops.
                      • Prefer readable logic over overly compact expressions.

                      Examples

                      Controlled GCD

                      To generate two positive integers with a predefined greatest common divisor, first generate two coprime positive integers and multiply both by the target GCD. This constructs the required property directly instead of repeatedly generating pairs and checking their GCD.

                      Guaranteed Integer Solutions

                      To generate a linear system with a unique integer solution, first define an integer solution vector and an invertible coefficient matrix. Then calculate the corresponding right-hand side vector. This constructs a system with the intended solution instead of generating a system and hoping its solution has the required properties.

                      Common Misunderstandings

                      Shorter algorithms are always better

                      Clarity and reliability are more important than compact code. An algorithm should make its generation rules, constraints, and calculations easy to follow.

                      Random values automatically produce suitable exercises

                      Random generation alone does not ensure valid or appropriate question variations. Use advanced logic in Algorithm Mode to define the mathematical properties and constraints your exercises require.

                      Related Concepts

                      • Understanding Algorithm Mode in Nubric.
                      • Algorithm Mode in Nubric: guided tutorials.
                      • Nubric glossary of commands.

                      Was this article helpful?

                      Give feedback about this article

                      Patterns and best practices for Algorithm Mode in Nubric

                      What It Is Why It Matters How It Works Stage 1: Generate base values first Stage 2: Enforce constraints with loops Stage 3: Compute solutions after validating the values Stage 4: Control edge cases early Stage 5: Use helper functions when logic repeats Key Rules or Behaviours Examples Controlled GCD Guaranteed Integer Solutions Common Misunderstandings Shorter algorithms are always better Random values automatically produce suitable exercises Related Concepts

                      Empowering STEM education

                      MathType

                      • Office Tools
                      • LMS
                      • XML
                      • HTML

                      WirisQuizzes

                      Nubric

                      Integrations

                      Solutions

                      • Education
                      • Publishing houses – platforms and interactive
                      • Publishing houses – Print and digital
                      • Technical writers

                      Pricing

                      Downloads

                      Blog

                      • Success stories

                      About us

                      • Careers
                      • Partnership

                      Contact Us

                      Contact Sales

                      European union (European Regional Development Fund) and 1EdTech (TrustEd Apps Certified)
                      • Cookie Policy
                      • Terms of Use
                      • Privacy Policy / GDPR
                      • Student Data Privacy
                      • Compliance
                      • Cookie Settings

                      © Wiris 2026

                      Expand