Use Matching Randomized Values from Two Lists

You can use the WebAssign pick() function to randomly select an index value in order to use corresponding randomized values from two or more lists, for example, an element and its atomic number.

Ensure that the number of items in each list is the same.

Tip A common use of this method is to select a value that is displayed in the question, and a corresponding value that is used to determine the answer key.
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.

This procedure describes a recommended way to select matching items from two or more lists. This can be accomplished in several ways, but the method described here is less prone to error than some other approaches.

  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.

Note The code in each of the following steps must be in an <EQN> or <eqn> tag.
  1. Assign the lists of items from which to select to array variables.

    Ensure that each array lists the corresponding items in the same position, so the nth item from one list corresponds to the nth item in the other list.

    For example:

    @symbols = ('He','Ne','Ar','Kr','Xe','Rn');
    @weights = (4.00, 20.18, 39.95, 83.80, 131.29, 222);
  2. Use the pick() function to randomly select an index value for the list items.
    Use the following syntax:
    pick(1,0..$#array_name)

    where array_name is the variable name — without the @ sign — of one of the arrays containing values that you want to select.

    Note Remember that pick() returns a list of values.

    For example:

    ($thiselement) = pick(1,0..$#symbols);
  3. Display the array elements using the randomly selected index.
    Use the following syntax:
    $array_name[$index_name]

    For example:

    $a = $symbols[$thiselement];
    $b = $weights[$thiselement];

Example Question Selecting Atomic Symbols and Element Names

The following table summarizes an actual question.

QID

1929960

Name

Mode

Fill-in-the-Blank

Question

<eqn>
@symbols = ('He','Ne','Ar','Kr','Xe','Rn');
@elements = ('Helium','Neon','Argon','Krypton','Xenon','Radon');
($idx) = pick(1,0..$#symbols);
''
</eqn>
The atomic symbol for <eqn $elements[$idx]> is: <_>

Answer

<EQN $CASE=1; $symbols[$idx]>

Display to Students

Question as displayed to students

Example Question Selecting Number Set Names and Definitions

The following table summarizes an actual question.

QID

1928870

Name

Mode

Fill-in-the-Blank

Question

<eqn>
@terms = (
  "Integer \t Integers",
  "Natural \t Naturals \t Natural Number \t Natural Numbers",
  "Whole \t Wholes \t Whole Number \t Whole Numbers",
  "Rational \t Rationals \t Rational Number \t Rational Numbers" );
@definitions = (
  'All numbers that can be written without fractions or decimals.',
  'Numbers > 0 that can be written without fractions or decimals.',
  'Numbers &#8805; 0 that can be written without fractions or decimals.',
  'Numbers that can be written as the quotient of two integers.' );
($idx) = pick(1,0..$#terms);
''
</eqn>
Name the set.<br/>
<eqn $definitions[$idx]><br/><_>

Answer

<EQN $terms[$idx]>

Display to Students

Question as displayed to students