Randomizers¶
Randomization makes each student see different numbers in a question while ensuring the question remains solvable. Here are the key functions.
randint($min, $max)¶
Returns a random integer between $min and $max (inclusive).
$a = randint(2, 9); // random integer from 2 to 9
$n = randint(-5, 5); // random integer from -5 to 5
rand($min, $max)¶
Returns a random float (decimal) between $min and $max.
$x = rand(0, 1); // random float like 0.4327
$rate = rand(0.01, 0.15); // random rate between 1% and 15%
pickone($value1, $value2, ...)¶
Returns one of the provided values at random.
$sign = pickone(1, -1);
$word = pickone("increasing", "decreasing");
$coeff = pickone(2, 3, 5, 7);
pickunique($n, $value1, $value2, ...)¶
Returns an array of $n values chosen from the list without repetition. Useful for selecting distinct options.
$choices = pickunique(3, 1, 2, 3, 4, 5, 6);
// might return array(4, 1, 6)
shuffle($array)¶
Randomizes the order of an array. Commonly used to randomize the order of multiple choice options.
$options = array("increasing", "decreasing", "constant", "undefined");
shuffle($options);
Avoiding Bad Values¶
Always check that random values don't produce degenerate cases (division by zero, negative radicands, etc.):
do {
$a = randint(-5, 5);
} while ($a == 0); // ensure $a ≠ 0
For quadratics, ensure the discriminant is non-negative if you want real roots:
do {
$b = randint(-6, 6);
$c = randint(-9, 9);
} while ($b**2 - 4*$c < 0);
Ensuring Distinct Values¶
When you need multiple different values:
$a = randint(1, 9);
do {
$b = randint(1, 9);
} while ($b == $a);
Or use pickunique:
$pair = pickunique(2, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$a = $pair[0];
$b = $pair[1];
Seeding and Reproducibility¶
Each student receives a unique seed that determines their random values. The same seed always produces the same values for a given student — so if a student starts a new attempt, they may see the same values (depending on how attempts are configured). This is by design: it allows instructors to verify a student's specific values when reviewing their work.
Example: Full Randomized Question Setup¶
// Pick two distinct nonzero integers
$pair = pickunique(2, -5, -4, -3, -2, 2, 3, 4, 5);
$a = $pair[0];
$b = $pair[1];
// Compute answer
$product = $a * $b;
$sum = $a + $b;
Question text:
<p>Find a quadratic with roots `$a` and `$b`.</p>
<p>Write it in the form `x^2 + bx + c`.</p>