Note: You are currently viewing documentation for Moodle 3.6. Up-to-date documentation for the latest stable version of Moodle is likely available here: question/type/stack/doc/doc.php/Authoring/KeyVals.md.

question/type/stack/doc/doc.php/Authoring/KeyVals.md

From MoodleDocs

STACK Question Variables

This field is for variables that are used later in the question. The values can be quite general: integers, floating point numbers, symbols or expressions, or random selections of any of the preceding elements. The variables can be access in other fields of the question by including them in CAStext. The assignment operator is the colon (:), as is consistent with the maxima program which processes expressions in STACK. These assignments can be used to build up complicated expressions from simple ones using maxima syntax.

Simple Assignments

An example of a simple variable assignment is

p:5;

This assigns the value of 5 to the variable p. In your question text, but example the appearance of @p@, will be replaced by 5. The line need not end in a semicolon, but its good practice in case you want to cut and past the assignments into a maxima session for testing.

q:x^2+2;

This assigns the symbolic expression representing 'x2+2' to the variable q. This can be used as a model answer, for instance or part or a later expression.

Random Assignments

The significant power of question variables becomes evident when they are used to generate randomly chosen values for a question by means of the rand operator. Using rand with an integer argument produces a random integer between 0 and one less than that number. In other words, rand(4) would randomly return either 0, 1, 2, or 3. An assignment like

x:rand(6)+1;

would produce a random number between 1 and 6 inclusive that could be used in other parts of the question or answer.

IMPORTANT NOTE: If you use random variables in your Question Variables you need to be sure and include them in the Question Note as well. Otherwise, you (and Moodle) won't be able distinguish between the various question variance when analyzing the questions and reporting results.