## WeBWorK Problems

### Randomly selecting options for new_checkbox_multiple_choice() ### Randomly selecting options for new_checkbox_multiple_choice()

by Cindy Loten -
Number of replies: 2

Hello,

I have a style question.

I've pasted a minimal example below for new_checkbox_multiple_choice() that does the following: randomly selects 3 entries from an array of 4 correct answers and randomly selects entries 3 from an array of 4 wrong answers to be displayed as options for the students to select.

Is there a smarter/easier way to accomplish what I've done? As far I can tell, there's no built in option in new_checkbox_multiple_choice() to accomplish this task.

########################################################################

DOCUMENT();

"PGstandard.pl",

"MathObjects.pl",

"PGML.pl",

"PGchoicemacros.pl",

"PGcourse.pl",

);

# Print problem number and point value (weight) for the problem

TEXT(beginproblem());

###########################

# Setup

@correct = (

"correct1 $BR", "correct2$BR",

"correct3 $BR", "correct4$BR",

);

$num_cor =3; @temp_cor = (0..$#correct); #array of indices of @correct

@slice_cor = (map { splice(@temp_cor, random(0, $#temp_cor), 1) } 1 ..$num_cor);

#randomly select $num_cor correct options to display @cor_print = @correct[@slice_cor]; @wrong = ( "wrong1$BR",

"wrong2 $BR", "wrong3$BR",

"wrong4 $BR" );$num_wrg =3;

@temp_wrg = (0..$#wrong); #array of indices of @wrong @slice_wrg = (map { splice(@temp_wrg, random(0,$#temp_wrg), 1) } 1 .. $num_wrg); #randomly select$num_wrg wrong options to display

@wrg_print = @wrong[@slice_wrg];

$mc = new_checkbox_multiple_choice();$mc -> qa (

"From the list below, select every sentence that must be true.",

$cor_print,$cor_print,

$cor_print, );$mc -> extra(

$wrg_print,$wrg_print,

$wrg_print, ); #$mc -> makeLast("None of the above");

###########################

# Main text

BEGIN_PGML

[@ $mc -> print_q() @]*** [@$mc -> print_a() @]***

END_PGML

############################

# Show which answers are correct and which ones are incorrect, set to 0 for no information

$showPartialCorrectAnswers = 0; ANS( checkbox_cmp($mc->correct_ans() ) );

############################

# Solution

BEGIN_PGML_SOLUTION

END_PGML_SOLUTION

COMMENT('Uses PGML and Math Objects.');

ENDDOCUMENT(); ### Re: Randomly selecting options for new_checkbox_multiple_choice()

by Glenn Rice -
The only good answers that I can give you are answers that involve using tools that are not yet released. PG 2.18 will have a parserCheckboxList.pl macro that will handle check box answers much better than new_checkbox_multiple_choice. Also, there will be a "random_subset" method in PGauxiliaryFunctions.pl (which is loaded by PGstandard.pl) that can select subsets for you. That will replace NchooseK from PGchoicemacros.pl.

Using 2.17 and before tools, what you have looks good. 