Use Randomized Values from a Table

You can use the WebAssign® randomization functions to select values from a table stored as a two-dimensional array. This is particularly useful in subjects like chemistry where simple substitution of independent values like a coefficient is often not possible.

To select values randomly from a table:

  1. If needed, open your question in the Question Editor.
  2. Store question values that you want to choose from in a Perl array.
    Note: Use the WebAssign @dat array to view the values as a table in the Question Previewer.
  3. Use WebAssign randomization functions to select values from the array to use in your question.
    Best Practice: Select index values between 0 and $#dat. This ensures that the index is valid even when you add or remove rows from the @dat array.
See the examples and prerequisite topics for specific information.

Example Question Using Array and Randomization

The following table summarizes an actual question.

QID
1950001
Name
Template2 RAND.ARRAY
ModeMultiple-Choice
Question
<eqn>
# weight in Newtons
$weight = randnum(495,940,1);

# calculate weight on other planets
@dat = ( ['Mercury' , decform($weight * 0.348, 0) ],
         ['Venus'   , decform($weight * 0.907, 0) ],
         ['Mars'    , decform($weight * 0.377, 0) ],
         ['Jupiter' , decform($weight * 2.364, 0) ],
         ['Saturn'  , decform($weight * 1.064, 0) ],
         ['Uranus'  , decform($weight * 0.889, 0) ],
         ['Neptune' , decform($weight * 1.125, 0) ] );

# pick three indices for @dat
@selected = pick(3, 0..$#dat);
''
</eqn>
If you weighed <eqn $weight> N on Earth:<br>
<ul>
<li>Your weight on <eqn $dat[$selected[0]][0]> would be <eqn $dat[$selected[0]][1]> N</li>
<li>Your weight on <eqn $dat[$selected[1]][0]> would be <eqn $dat[$selected[1]][1]> N</li>
<li>Your weight on <eqn $dat[$selected[2]][0]> would be <eqn $dat[$selected[2]][1]> N</li>
</ul>
On which planet would your mass be the least? <_>
Answer
<EQN $ORDERED=5; 'Earth'>
<EQN $dat[$selected[0]][0]>
<EQN $dat[$selected[1]][0]>
<EQN $dat[$selected[2]][0]>
Your mass would be the same
Display to Students
Question as displayed to students

Randomized chemPad Example Using a Table

The following table summarizes an actual question.

QID

1931882

Name



                    

Mode

Fill-in-the-Blank

Question

<eqn>
@dat = (
  ['2 CO + O_2 ->','2 CO_{2}'],
  ['2 H_2 + O_2 ->','2 H_{2}O'],
  ['4 Fe + 3 O_2 ->','2 Fe_{2}O_{3}'],
  ['SO_2 + H_2O_2 ->','H_{2}SO_{4}']
);
# pick an index to use for the array
($a) = pick(1,0..$#dat);
''
</eqn>
Complete and balance the following chemical equation. <br />
<h:reaction><eqn $dat[$a][0]></h:reaction> <br />
<_>

Answer

<EQN $PAD='chem'; $CHEM='fmla'; $dat[$a][1]>

Display to Students

Question as displayed to students

Example Multiple-Select Question with a Random Number of Correct Answers

The following table summarizes an actual question.

Note: In this example, the second column of @dat displays as ARRAY(0xnnnnnnnn) in the Array tab of the Question Previewer. This is because the answer key is stored as an array within the @dat array.

QID

1930563

Name



                    

Mode

Multiple-Select

Question

<eqn>
@dat = (
  ['white',[1,3,4]],
  ['yellow',[1,3]],
  ['magenta',[1,4]],
  ['cyan',[3,4]]
);
($a) = pick(1,0..$#dat);
''
</eqn>
What primary colors of light can be combined to make <eqn $dat[$a][0]> light?<br>
<_>

Answer

<EQN $ORDERED = $dat[$a][1]; ''>red
indigo
green
blue
orange

Display to Students

Question as displayed to students