RandVars

RandVars

RandVars[ n ] randomly selects n different variable names.

Usages

RandVars[ n ]

RandVars[ n ] returns a list of n different single-letter variables, chosen from a, m, n, p, s, t and x. Letters that are easily confused with other symbols or in handwriting, such as o, v, q and i, are never chosen. With n = 1, a single variable is returned instead of a list. Each time the exercise is generated, a new choice is made, so the outputs below are only examples.

Example in out
A single random variable. RandVars[ 1 ] for example m
3 different variables. RandVars[ 3 ] for example {p, x, m}
Use the variables in an expression. v := RandVars[2]
e := v[[1]] + 2 v[[2]]
for example e = m+2n

RandVars[ n, {var1, var2, ...} ]

With a second argument, the variables are chosen from the given list instead.

Example in out
2 variables from p, q and r. RandVars[ 2, {p, q, r} ] for example {r, p}
Longer names are possible. RandVars[ 2, {alpha, beta, gamma} ] for example {gamma, alpha}
Options

Exclude

A list of letters that must not be chosen. Only used without a second argument.

Syntax Example out
RandVars[ n, Exclude=>{var1, var2, ...} ] RandVars[ 3, Exclude=>{x, t} ] for example {p, m, a}
Details
  • At most 7 variables can be chosen without a second argument, and fewer when letters are excluded.
  • Zero variables: RandVars[ 0 ] returns {}.
Invalid input Result
RandVars[ 8 ] Error: the 1st argument exceeds the number of available variables 7
RandVars[ 4, {p, q} ] Error: the 1st argument exceeds the number of available variables 2
RandVars[ 2, Exclude=>x ] Error: The associative argument 'Exclude' should be a list
RandVars[ x ] The script stops with an internal error: n must be an integer.

See also: RandomSelect, RandomInt