Skip to main content
Chemistry LibreTexts

7.5: Testing Randomization and Edge Cases

  • Page ID
    557411
  • ​

    ​

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Use this section when your problem works for one seed but you need to make sure it works for many seeds. Randomization is what makes WeBWorK problems reusable: each student gets different numbers. But different numbers can produce impossible biology, division by zero, or answers that do not make sense. Testing multiple seeds catches these problems before students encounter them.

    Think of each seed as a different student sitting down with your question. You wrote the problem with one set of numbers in your head, but the randomization engine will generate hundreds of variants. Your job is to make sure every variant tells a coherent story with plausible numbers and a correct answer that makes sense.

    Why randomization testing matters

    • Same problem, different seed, different failure. A problem that works perfectly with seed 1234 might produce a negative concentration with seed 5678.
    • Students will see many different variants. You only tested a few. The variant you did not test is the one that will confuse a student at midnight before the deadline.
    • Guard patterns (described below) prevent bad values at the source, so you do not have to test every possible seed by hand.

    How to sweep variants

    In the browser editor, change the seed field and re-render. Try at least five to ten different seeds for any problem that uses randomization. For each variant, check the following:

    • Does the story still make sense with these numbers?
    • Are all numbers plausible for the biology? (No negative concentrations, no probabilities above 1, no impossible rates.)
    • Does the rounding produce readable numbers? (Watch for long repeating decimals like 0.333333 that should be rounded.)
    • Is the correct answer reasonable? (If the answer is 0, or negative, or astronomically large, something is wrong.)

    If you are using the command-line lint script, you can render with a specific seed using the -s flag:

    python3 lint_pg_via_renderer_api.py -i private/myproblem.pg -s 5678 -r > /tmp/pg_render.html
    

    Guard patterns: preventing bad values

    Guard patterns are short code snippets that prevent randomization from producing values that would break the problem (see Advanced Randomization Techniques for additional randomization patterns and techniques). Add them in the setup section of your PG file, before the PGML block.

    Avoid zero denominators

    If a random variable might be zero and you later divide by it, the problem will crash for some students. Prevent this with a do/while loop or the non_zero_random function.

    # Option 1: do/while loop
    do { $b = random(-5, 5, 1); } while ($b == 0);
    
    # Option 2: non_zero_random (simpler)
    $b = non_zero_random(-5, 5, 1);
    

    Avoid repeated values

    When two variables should have different values (for example, two different enzyme concentrations to compare), use a do/while loop to ensure they are not equal.

    $a = random(2, 9, 1);
    do { $b = random(2, 9, 1); } while ($b == $a);
    

    Avoid invalid domains

    Some mathematical operations are undefined for certain inputs. If your problem takes the logarithm or square root of a random value, make sure that value cannot be zero or negative.

    # Ensure $x is positive before using log($x)
    do { $x = random(-5, 5, 1); } while ($x <= 0);
    

    Sort hash keys before random selection

    Perl hash key order is not deterministic. If you select a random key from a hash, the same seed can produce different results on different runs unless you sort the keys first.

    @sorted_keys = sort keys %items;
    $selected = list_random(@sorted_keys);
    

    Shuffle after all items are defined

    A common mistake is to shuffle an array before all of its elements are assigned. This produces empty or missing items in the shuffled result. Always follow this three-step pattern:

    # Step 1: Define all items first
    $item1 = "Adenine";
    $item2 = "Thymine";
    $item3 = "Guanine";
    
    # Step 2: Create the array
    @all_items = ($item1, $item2, $item3);
    
    # Step 3: Shuffle (only after all items are in the array)
    @temp = @all_items;
    @shuffled = ();
    while (@temp) {
        $index = random(0, $#temp, 1);
        push @shuffled, splice(@temp, $index, 1);
    }
    

    Boundary value testing

    Beyond sweeping random seeds, test specific values near the boundaries of expected ranges. These are the values most likely to cause problems.

    • Test values near zero, one, and other natural breakpoints in your problem.
    • Test the smallest and largest values in your random range.
    • Test values that produce simple fractions or exact answers to confirm rounding behaves as expected.

    For biology problems, pay special attention to domain-specific constraints:

    • Concentrations: Never negative. Usually between 0 and a realistic maximum for the context (millimolar, micromolar, etc.).
    • Probabilities: Always between 0 and 1. Never above 1, never below 0.
    • pH values: Between 0 and 14.
    • Rates: Non-negative and of a reasonable magnitude for the biological process.
    • Percentages: Between 0 and 100.

    What to watch for: a checklist

    What to watch for: a checklist: check, what could go wrong, and how to test.
    Check What could go wrong How to test
    Negative concentrations Random subtraction goes below zero Try seeds that produce small values near the lower end of the range
    Impossible probabilities Probability exceeds 1 or goes below 0 Check boundary cases where the computed probability approaches 0 or 1
    Awkward rounding Values like 0.333333... displayed without rounding Set explicit rounding in the problem and verify the display
    Repeated answer choices Two random choices get the same value Use a do/while guard to ensure uniqueness
    Division by zero Random denominator is zero Use non_zero_random or a do/while guard
    Story does not match numbers Random values make the scenario implausible Read each variant as a student would and ask whether the story makes sense

    Apply it today

    Pick one problem with randomization. Render it with five different seeds. For each variant, ask yourself: "Would this make sense to a student?" If any variant produces impossible numbers or a confusing story, add a guard pattern to prevent it. A few minutes of seed-sweeping now saves a confused email from a student later.


    This page titled 7.5: Testing Randomization and Edge Cases was last modified on Fri, 27 Feb 2026 15:39:11 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Neil R. Voss.