# 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.

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 Template2 RAND.ARRAY Multiple-Choice  # 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); '' If you weighed N on Earth:
• Your weight on would be N
• Your weight on would be N
• Your weight on would be N
On which planet would your mass be the least? <_>  Your mass would be the same ## Randomized chemPad Example Using a Table

The following table summarizes an actual question.

 QID 1931882 Fill-in-the-Blank  @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); '' Complete and balance the following chemical equation.

<_> ## 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 Multiple-Select  @dat = ( ['white',[1,3,4]], ['yellow',[1,3]], ['magenta',[1,4]], ['cyan',[3,4]] ); ($a) = pick(1,0..$#dat); '' What primary colors of light can be combined to make light?
<_> red indigo green blue orange 