## WeBWorK Problems

### Selecting a random set of questions from a list

by Steven McKay -
Number of replies: 2
Hi,
First of all, let me apologize if this is clearly written down somewhere, but I've looked with no success (or I did not understand what I was looking at).

I would like to create a question with several parts. However, the student should only see a randomized subset of the parts. For example, my question has 6 parts, but the student sees 3 of the 6. Then the answers are checked appropriately for these 3. I know that there is a way to do this but have not found a code snippet to do it.

A code snippet that uses MathObjects as well would be great, but I'll take anything.

Is there standard code for this? If so, just roll your eyes at my searching incompetence, and if you would, send me the url. If not, any suggestions?

Thanks,

S. M.

### Re: Selecting a random set of questions from a list

by Gavin LaRose -
Hi Steven,

Depending on how long and complicated your parts are, it sounds to me as if this could be easily done with a simple conditional or by randomly picking between the parts. As an example, I'll assume that the six parts are all derivatives, though that's probably not what you had in mind. In this case I would define all six parts in an array:

@parts = ( Compute("x^2"), Compute("sin(x^2)"), Compute("e^(x^2)"), Compute("ln(x^2)"), Compute("cos(1/x)"), Compute("tan(x^2)") );

and then pick three of them
@selected = @parts[ NchooseK(6,3) ];

then we can display them:
Context()->texStrings;
BEGIN_TEXT
Find the derivatives of each of the following:
$BR (a) $$selected[0]$$: \{ ans_rule(35) \}$BR
(b) $$selected[1]$$: \{ ans_rule(35) \}
$BR (c) $$selected[2]$$: \{ ans_rule(35) \} END_TEXT Context()->normalStrings; and check the answers: ANS($selected[0]->D()->cmp() );
ANS( $selected[1]->D()->cmp() ); ANS($selected[2]->D()->cmp() );

If the parts were more complicated, we could just have a big conditional in the problem to decide which to display and mark. In pseudo code, this might be

@which = NchooseK(6,3);
if ( 0 in @which ) {
set up problem part, display it, mark answer
}
if ( 1 in @which ) {
etc.
}
and so on.

Gavin