Note: You are currently viewing documentation for Moodle 2.9. Up-to-date documentation for the latest stable version of Moodle may be 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.

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 variants when analyzing the questions and reporting results.

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. A random floating point number is generated by including a number with a decimal point as an argument. It returns a floating point number between 0 and the given value. Thus,

y:rand(10.0)-5;

would return a random floating point number between -5.0 and 5.0.

The rand function can also return a random element in a list. So, for instance,

q:rand([a,b,c,d,e]);

would return a random selection of a, b, c, d, or e. Using this facility, one could build random symbolic expressions in the questions for each student.

Variations on rand

There are a couple of specialized rand functions you can use for unique situations. The function rand_with_step takes three arguments:

  • the lower bound of the number
  • the upper bound of the number
  • the step size

Thus rand(1,5,2) would randomly choose 1, 3, or 5. A related function is rand_with_prohib. It also take three arugments:

  • the lower bound of the numbers
  • the upper bound of the numbers
  • a list of values to be excluded

For instance, rand_with_prohib(-5,5,[0]) would generate random integers between -5 and 5 inclusive, but not including 0.