Randomized variables can be incorporated into any question. Rules and recommendations for effective use of randomized variables are given below, illustrated by related examples.
The code that creates and manipulates the randomized variables is contained in the \code{} section of the question. Each variable in the code section is designated with a $ symbol, such as $m, $ans, and $b2. The first character after the $ symbol must be a letter. Each code statement is of the form
$variable=expression;
The final semicolon separating multiple variable definitions in a list is not optional.
Note: TeX comments (%) cannot appear in the argument to \code. To include comments, use the comment control sequence.
The usual arithmetic operations and functions can be used in the expression portion of the code statement. However, unlike most other programming languages, variables cannot be redefined in terms of themselves (for example, the statement $a=$a+1; is not allowed). For a description of the additional functions that can be used in the code sections, see Functions within Algorithms.
To use the variables in other sections of the question, enclose them in the \var control sequence. For example, to refer to the variable $a, use:
\var{a}
Consider the following simple example.
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{(\var{c}-\var{b})/\var{a}}
\code{
$a=rint(12)+1;
$b=rint(12)+1;
$c=rint(12)+1;
}
\end{question}
The above code randomly determines integer coefficients $a, $b, and $c between 1 and 12. The answer is calculated in the \answer section.
Although the above example does not cause errors, there are several side effects:
The answer is displayed in the form (7-3)/5 (for example). This provides an indication of the method for obtaining the answer, which may be inappropriate.
The above format can return answers of the form (7-7)/5, which is more complicated than the simplified form, 0.
To avoid these issues, rewrite this example as follows, using a new variable $ans (so that you return only the final answer).
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{\var{ans}}
\code{
$a=rint(12)+1;
$b=rint(12)+1;
$c=rint(12)+1;
$ans=($c-$b)/$a;
}
\end{question}
In this code, there are variable type issues.
All variables are floating-point variables unless they are wrapped in an int() or rint() statement to make them integers (for example, $m=int(14*5);), or in double quotes "" to make them strings (for example, $s="north";).
The statement $a=rint(11); creates an integer variable, but the line $a=rint(11)+1; creates a floating-point variable as a result of the +1 operation. The same is true for the definitions of $b and $c. Using the above question, equations in the form 2.0x+9.0=4.0, which is correct, but not as simple as possible. Change the code so that $a, $b, and $c are integers.
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{\var{ans}}
\code{
$a=int(rint(12)+1);
$b=int(rint(12)+1);
$c=int(rint(12)+1);
$ans=($c-$b)/$a;
}
\end{question}
There is one more problem. The variable $a can equal 1, in which case the equation has the form 1x+5=8. To avoid this situation, impose the condition that $a be an integer between 2 and 12.
Although $c can be 0 or negative, if $b is 0 or negative the equation is again unsimplified. Generalize the code to allow $c to be any integer between -12 and 12.
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{\var{ans}}
\code{
$a=int(rint(11)+2);
$b=int(rint(12)+1);
$c=int(rint(25)-12);
$ans=($c-$b)/$a;
}
\end{question}
There is another minor problem. The answer $ans is a floating-point number. If the student gives an incorrect response, the correct answer is displayed as a (potentially inexact) floating-point number, for example, .666666667, instead of 2/3. The system grades the question properly, but the displayed feedback may not have the correct form. To solve the problem, use a string variable to display the answer as a fraction.
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{\var{ans}}
\code{
$a=int(rint(11)+2);
$b=int(rint(12)+1);
$c=int(rint(25)-12);
$num=int($c-$b);
$ans="$num/$a";
}
\end{question}
Note: Writing $ans="($c-$b)/$a"; displays answers of the form (11-6)/3. First, calculate the numerator and store it as an integer in $num, and then create the fraction with the string "$num/$a".
This last change introduces a new problem. The system displays correct answers of the form 4/2 or 3/3. To make integer answers display as integers, implement one more change.
\begin{question}{Formula}
\qutext{Find the solution of the linear equation
$\var{a}x+\var{b}=\var{c}$.}
\answer{\var{ans}}
\code{
$a=int(rint(11)+2);
$b=int(rint(12)+1);
$c=int(rint(25)-12);
$num=int($c-$b);
$ansint=int($num/$a);
$ans=if(int(eq($num/$a,$ansint)),"$ansint","$num/$a");
}
\end{question}
By using the intermediate variable $ansint and the string "$ansint" in the last statement (instead of directly using the string "int($num/$a)", which produces answers of the form "int(6/3)"), integers display properly.
This final version allows some nonreduced fractions, for example, 2/4, to appear as answers, but, in general, both the problem and the answer appear in a simple, standard form.
Ensure that all possible values of randomized variables are valid in the question text and the answer, and that the response will grade correctly.
If you are using integers in the question text or answer, ensure that they are formatted properly.
Define randomized variables so that you avoid expressions like 1x, 0x, x+-3, or -3/-4. If these values are required, write the question so that they display properly.
Write the code so that if the student answers incorrectly, the correct answer appears in a standard form. In general, you must use strings, as shown in the final example.