WeBWorK uses of the language perl with extra packages to author questions.
A parameter/variable in Perl starts with “$”, for example $a, $integer, $var.
Supported by the PGrandom package, in WeBWork random variables can be defined.
One random number
-
Generates a random number between the initial and final values in steps of increment.
If the increment is 0 or less, then random chooses a floating point number between
$initialand$finalusing a small increment.$incrementdefaults to 1, if it is not supplied as an argument.Example:
$m=random(-2, 20, 3);generates a number among -2, 1, 4, …, 19 and stores it in the variable$m. Because the step is 3. -
Generates a random number between 0 and $final.
$finaldefaults to 1 if that argument is omitted.Example:
$r = rand(1);generates a number between 0 and 1 and store it in the variable$r. -
Generate a nonzero random number between the initial and final values.
Example:
$coef = non_zero_random(2,7);generates a nonzero integer between 2 and 7 and stores it in the variable$coef.
Two or more different random numbers
-
Generate two or more different random numbers
$old is a predefined random number.
Example:
do { $new = random(1, 6); } until ( $new != 3 );generates a number between 1 and 6 but not equal to 3 and store it in the variable$new. -
Generate a third different random number number
$firstandsecondare predefined random numbers.Example:
do { $third = random(1, 6); } until (($third != 3) and ($third != 5);generates a number between 1 and 6 but not equal to 3 and 5, and store it in the variable$third.
Random variables
To generate a random variable, we can use the RandomVariableName function supported by the package PCCmacros.pl.
-
Generate one random variable.
In order to use the variable
$varin formula, the second line is necessary. -
Generate two different random variables
$varxis a predefined variable. Note thatadd()should be used for extra variables.Example: The following codes will generate two different variables
$varxand$vary.
A random element from a list
-
Randomizing from a list
The element
$xican be a number, a letter or an expression.Example:
$Days = list_random(28, 29, 30, 31)randomly assigns a number from the list to the variable$Days. -
Randomizing from an array
Example: Randomly assigns a number or the letter
from the list to the variable$Days.