Wiris

Documentation / LearningLemur

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

                      How to generate two random irreducible fractions

                      Reading time: 1min

                      Use this method when you want students to compute the sum of two fractions and ensure that each generated fraction is already irreducible (i.e., simplified before the operation).

                      Following this guide, you will generate two random fractions whose numerators and denominators are coprime, guaranteeing that both fractions are irreducible before students calculate their sum.

                      See it in action: Watch how this logic is implemented inside Nubric:

                      Your browser does not support HTML5 video.

                      Before you begin

                      Requirements

                      • Basic knowledge of how to create a Nubric question
                      • Familiarity with adding an algorithm to generate random variables

                      Steps

                      Generate the first numerator

                      a = random(1,9)

                      This defines the numerator of the first fraction.

                      Generate a compatible denominator

                      B = {b with b in 2..9 where gcd(a,b) = 1}
                      b = random(B)

                      This creates a set of denominators coprime to a, ensuring the fraction is irreducible.

                      Generate the second numerator

                      c = random(1,9)

                      Generate the second denominator

                      D = {d with d in 2..9 where gcd(c,d) = 1 && d != b}
                      d = random(D)

                      This ensures:

                      • The second fraction is irreducible.
                      • The denominators are not identical.

                      Define the solution

                      sol = a/b + c/d

                      This stores the correct answer for grading.

                      Verify it worked

                      • Preview the question multiple times.
                      • Confirm that each generated fraction is already simplified.
                      • Check that no denominator equals 0.
                      • Ensure the sum is computed correctly.

                      Full algorithm (copy-paste version)

                      Use the complete version below if you want to copy the logic directly into your question algorithm:

                      # Generate the first fraction
                      a = random(1,9)    
                      
                      B = {b with b in 2..9 where gcd(a,b) = 1}  
                      b = random(B)               
                      
                      # Generate the second fraction
                      c = random(1,9)   
                      
                      D = {d with d in 2..9 where gcd(c,d) = 1 && d != b} 
                      d = random(D)                  
                      
                      # Compute the solution
                      sol = a/b + c/d

                      Options and variations

                      • If you want to allow identical denominators, you can remove the condition d != b from the second denominator set.
                      • If you want larger fractions, you can increase the interval (e.g., 1..15 instead of 1..9), but test performance to avoid excessive regeneration.
                      • If you want to enforce simplified student answers, you can combine this pattern with answer validation constraints in grading logic.

                      Common errors

                      Fractions are not irreducible.
                      Check that gcd(a,b)=1 is correctly written in the set definition

                      Infinite regeneration loop.
                      Ensure the denominator interval contains enough values to satisfy the coprimality condition

                      Unexpected duplicate fractions.
                      Add additional constraints if you need both fractions to differ entirely

                      Related

                      • Understanding Advanced Logic in Nubric
                      • Common Patterns and Best Practices
                      • Glossary of Commands

                      Was this article helpful?

                      Give feedback about this article

                      Related Articles

                      • Greatest common divisor (gcd)
                      • Partial fractions
                      • Constant sequence

                      How to generate two random irreducible fractions

                      Before you begin Steps Verify it worked Full algorithm (copy-paste version) Options and variations Common errors Related

                      Empowering STEM education

                      MathType

                      • Office Tools
                      • LMS
                      • XML
                      • HTML

                      WirisQuizzes

                      Learning Lemur

                      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