Asia
Australia/New Zealand
Canada
Europe
Middle East
Africa
Latin America
India
Puerto Rico
United States

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.

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