Use Randomized Values from a List

You can use the WebAssign pick() function in your questions to select one or more distinct items at random from a list, for example, to select three molecules from a list of simple hydrocarbons.

Best Practice Assign each randomized value to a variable. Then use the variable to perform additional computation or to display the value in the question, answer key, or solution.
  1. If needed, open your question in the Question Editor.
    If Do this
    You know the question ID or name
    1. In the search box at the top of the page, select Question.
    2. Type the question name or ID and press Enter.
    You own the question
    1. Click Questions > My Questions.
    2. Click the question name.
    You organize your questions in folders
    1. Click Assignments > Folders and navigate to the folder with the question.
    2. Click the question name.
    You want to use advanced search
    1. Click Questions > Search Questions.
    2. Enter the search criteria you want to use.

      To view only your questions, click me next to Author.

    3. Click Search.
    4. If you own the question, click Edit next to your question.

      If you do not own the question, click View, then click Open in Editor under Previewer Tools.

  2. In an <EQN> or <eqn> tag, use the pick() function to select distinct values from a list.
    Use the following syntax:
    pick(n,list)
    where:
    • n is the number of different values you want to select, and must be less than or equal to the number of items in list.
    • list is either an array variable or a comma-delimited list of values.
    Note
    • Use a list of variables (in parentheses) or an array to store the values returned by pick().
    • The pick() function selects the specified number of different items from the list. For example, pick(2,'a','b','c') will never return ('a','a').

      However, pick(2,'a','b','b') might return ('b','b') because there are two different items in the list with a value of 'b'.

    For example: 

    # select a single color
    ($a) = pick(1,'red','orange','yellow','green','blue','violet');
    
    # select two different colors
    ($a,$b) = pick(2,'red','orange','yellow','green','blue','violet');
    # or
    @a = pick(2,'red','orange','yellow','green','blue','violet');
    
    # select two different integers between 15 and 25
    ($c, $d) = pick(2,15..25);
    
    # select one, two, or four values from an array
    @firstprimes = (1,2,3,5,7,11,13,17,19,23);
    ($x)    = pick(1,@firstprimes);
    ($x,$y) = pick(2,@firstprimes);
    @x      = pick(4,@firstprimes);

Example Question Using pick()

The following table summarizes an actual question.

QID

1931415

Name

pick() example 1

Mode

Numerical

Question

<eqn>
# select primes for $a and $b so students need not simplify
($a) = pick(1,7,11,13,17,19);
($b) = pick(1,61,67,71,73,79,83,89,97);
@color = pick(2,'red','purple','blue','green','yellow','orange');
''
</eqn>
A jar contains <eqn $b> marbles, of which <eqn $a> are <eqn $color[0]> and <eqn $b-$a> are <eqn $color[1]>. If you take 1 marble out of the jar without looking, what is the probability that the marble you choose is <eqn $color[0]>?
<br><_>

Answer

<EQN $FRACTION=1; fraction($a,$b)>

Display to Students

Question as displayed to students

Example Question Using pick()

The following table summarizes an actual question.

QID

1947949

Name

pick() example 2

Mode

Multiple-Choice

Question

<eqn>
@lanthanides = pick(3, 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu');
@distractors = pick(4, 'Actinides', 'Metalloids', 'Halogens', 'Alkali Metals', 'Alkaline earth metals', 'Transition metals', 'Noble gases');
''
</eqn>
The elements <eqn @lanthanides[0]>, <eqn @lanthanides[1]>, and <eqn @lanthanides[2]> are all:<br>
<_>

Answer

Lanthanides
<EQN @distractors[0]>
<EQN @distractors[1]>
<EQN @distractors[2]>
<EQN @distractors[3]>

Display to Students

Question as displayed to students