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
                      • Advanced Logic in Nubric
                      • Advanced Logic in Nubric

                      Common Patterns and Best Practices

                      Reading time: 2min

                      Advanced Logic enables powerful dynamic questions, but poorly structured algorithms can make questions difficult to maintain, debug, or extend. This article explains common patterns and best practices that help you write clearer, safer, and more reusable logic when creating Nubric questions.

                      What It Is

                      When writing algorithms for Nubric questions, 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.

                      Over time, several reusable patterns have emerged that make question algorithms easier to write and maintain. These patterns are not strict rules but recommended approaches that improve reliability and readability.

                      Why It Matters

                      Without consistent patterns, question algorithms can become:

                      • Difficult to debug.
                      • Fragile when random values change.
                      • Hard to understand months later.
                      • Difficult for colleagues to reuse.

                      Using common patterns helps ensure that:

                      • Algorithms remain readable.
                      • Constraints are clearly enforced.
                      • Question generation stays stable.
                      • Logic can be reused across different exercises.

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

                      How It Works

                      Most Nubric algorithms follow a similar conceptual structure.

                      Stage 1: Generate values first

                      Start by generating base random variables. Example concept:

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

                      These variables act as the foundation for the rest of the algorithm.

                      Stage 2: Enforce constraints with loops

                      When mathematical properties must be guaranteed, use loops to validate them. Example cases include:

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

                      Typical structure:

                      repeat
                         generate values
                      until condition_is_satisfied

                      or

                      while condition_not_met
                         regenerate values
                      end

                      Stage 3: Compute solutions after constraints

                      Once valid values are established, compute the solution variables. Example:

                      solution = a/b + c/d

                      Keeping solution logic separate improves readability and reduces errors.

                      Stage 4: Control edge cases early

                      Avoid problematic values from the start. Common examples:

                      • Excluding zero.
                      • Avoiding repeated denominators.
                      • Restricting ranges.

                      Example:

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

                      This prevents invalid calculations later in the algorithm.

                      Stage 5: Use helper functions when logic repeats

                      If the same logic appears multiple times, define a small function. Example:

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

                      This improves readability and simplifies future changes.

                      Key Rules or Behaviours

                      • Generate variables before computing solutions.
                      • Enforce mathematical constraints explicitly.
                      • Keep solution calculations separate from generation logic.
                      • Avoid unnecessary nested loops.
                      • Prefer readable logic over overly compact expressions.

                      Examples

                      Example 1: Controlled GCD.
                      Instead of generating two numbers and hoping their GCD matches a target, generate coprime numbers and scale them. Result: the GCD is guaranteed.

                      Example 2: Guaranteed integer solutions.
                      Instead of solving equations randomly, generate the solution first and derive the equation coefficients. Result: the system always has the desired solution.

                      Common Misunderstandings

                      Misconception: Shorter algorithms are better.
                      Clarification: Clarity and reliability are more important than compact code.

                      Misconception: Random values automatically produce good exercises.
                      Clarification: Without constraints, random generation often creates invalid or undesirable cases.

                      Related Concepts

                      • Understanding Advanced Logic in Nubric
                      • Advanced Logic Examples in Nubric
                      • Glossary of Commands

                      Was this article helpful?

                      Give feedback about this article

                      Related Articles

                      • Debugging (what to do if it isn't working)
                      • Tips to use with Microsoft Word
                      • Bugs & minor improvements resolved

                      Common Patterns and Best Practices

                      What It Is Why It Matters How It Works Stage 1: Generate values first Stage 2: Enforce constraints with loops Stage 3: Compute solutions after constraints Stage 4: Control edge cases early Stage 5: Use helper functions when logic repeats Key Rules or Behaviours Examples Common Misunderstandings 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