WeBWorK Problems

Unrandomizing answers in pgchoicemacros.pl

Unrandomizing answers in pgchoicemacros.pl

by Murphy Waggoner -
Number of replies: 6

How do I get the ? for No Answer to appear first in the drop-down menu. Instead of them being listed as 

    • ?
    • Descriptive Statistics
    • Inferential Statistics 

for each one as entered in the code, the possible answers are listed in random order with and random option listed first as if answers had been selected already.

Multiple choice questions in WeBWorK

 ww_version: 2.19 | pg_version 2.19

DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "PGchoicemacros.pl",
  "PGcourse.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 0;
install_problem_grader(~~&std_problem_grader);
# Make a new select list
$ma = new_select_list();
$ma->rf_print_q(~~&pop_up_list_print_q);
# Insert some  questions and answers
$ma -> ra_pop_up_list( [ 
No_answer => "  ?", 
Inferential => "Inferential Statistics", 
Descriptive => "Descriptive Statistics",
] );

$ma -> qa ( # each entry has to end with a comma
    'Collecting data',    'Descriptive',
    'Interpreting data',  'Inferential',
    'Summarizing data',   'Descriptive',
    'Describing the characteristics of a dataset',    'Descriptive',
    'Organizing a dataset',    'Descriptive',
    'Finding the mode of data',    'Descriptive',
    'Finding the mean of data',    'Descriptive',
    'Extrapolation',       'Inferential',
    'Inferring something about a population based on a sample',            'Inferential',
    'Applying results about a part of a population to the whole population',    'Inferential',
);
# Choose four of the question and answer pairs at random.
$ma ->choose(4);

BEGIN_TEXT
$PAR
For each activity, select whether it is a type of inferential or descriptive statistics. To prepare for this question, read Chapter 1 of the text.
$BR$BR
Note that you only have a limited number of tries for this question, meaning you can select "Submit Answers" only so many times before WeBWorK stops letting you change your answers.
$BR$BR
Also note that the answers are listed in random order to begin with.
$BR$BR
\{ $ma-> print_q \}

$PAR
END_TEXT
# Enter the correct answers to be checked against the answers to the students.
ANS(str_cmp( $ma->ra_correct_ans )   ) ;
COMMENT('Questions selected randomly from a larger set.  See source for full question list.');
ENDDOCUMENT();        # This should be the last executable line in the problem.


In reply to Murphy Waggoner

Re: Unrandomizing answers in pgchoicemacros.pl

by Glenn Rice -

I seem to recall that there is a way to specify that an answer always be first with the old PGchoicemacros.pl approach for creating pop up answers.  I don't remember what it is.  I don't really care either since newly written questions shouldn't use that deprecated approach.  You should use parserPopUp.pl instead.  The DropDown method from that macro will automatically add a "?" that is always first.  You also should use PGML instead of BEGIN_TEXT/END_TEXT.  Here is the problem rewritten to use the modern approaches that should be utilized with newly written problems.

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'parserPopUp.pl', 'PGcourse.pl');

$showPartialCorrectAnswers = 0;

# List of all question and answer pairs.
@questions = (
    [ 'Collecting data',                                                       'Descriptive Statistics' ],
    [ 'Interpreting data',                                                     'Inferential Statistics' ],
    [ 'Summarizing data',                                                      'Descriptive Statistics' ],
    [ 'Describing the characteristics of a dataset',                           'Descriptive Statistics' ],
    [ 'Organizing a dataset',                                                  'Descriptive Statistics' ],
    [ 'Finding the mode of data',                                              'Descriptive Statistics' ],
    [ 'Finding the mean of data',                                              'Descriptive Statistics' ],
    [ 'Extrapolation',                                                         'Inferential Statistics' ],
    [ 'Inferring something about a population based on a sample',              'Inferential Statistics' ],
    [ 'Applying results about a part of a population to the whole population', 'Inferential Statistics' ],
);

# Choose four questions and answer pairs at random.
@chosen = random_subset(4, @questions);

# Construct dropdowns for each answer.
@answers = map {
    DropDown([ [ 'Inferential Statistics', 'Descriptive Statistics' ] ],
        $_->[1])
} @chosen;

BEGIN_PGML
For each activity, select whether it is a type of inferential or descriptive
statistics. To prepare for this question, read Chapter 1 of the text.

Note that you only have a limited number of tries for this question, meaning you
can select "Submit Answers" only so many times before WeBWorK stops letting you
change your answers.

Also note that the answers are listed in random order to begin with.

[#
	[. 1. [$chosen[0][0]]: .] [. [_]{$answers[0]} .]*

	[. 2. [$chosen[1][0]]: .] [. [_]{$answers[1]} .]*

	[. 3. [$chosen[2][0]]: .] [. [_]{$answers[2]} .]*

	[. 4. [$chosen[3][0]]: .] [. [_]{$answers[3]} .]
#]*{ center => 0, align => 'll' }
END_PGML

COMMENT('Questions selected randomly from a larger set. '
        . 'See source for full question list.');

ENDDOCUMENT();

In reply to Glenn Rice

Re: Unrandomizing answers in pgchoicemacros.pl

by Murphy Waggoner -
The code I sent was not from a newly written problem, but rather from one of the hundreds of problems I had written in the past and now behave differently with the newest installation of WeBWorK on our servers.
In reply to Glenn Rice

Re: Unrandomizing answers in pgchoicemacros.pl

by Murphy Waggoner -
I just checked and this issue also appears when using the Library Browser and getting problems from the Open Problem Library.

For example, I just added Library/UVA-Stat/setStat212-Homework03/stat212-HW03-04.pg from the Open Problem Library to a problem set, and opened it. It uses PGchoicemacros.pl and randomizes the possible answers.

So, whether you care about this issue or not, it is a problem that I am hoping there is a solution to other than having to rewrite all the problems that use PGchoicemacros.pl in my college's repositories and rewriting all the problem I get from the Open Problem Library using Library Browser in WeBWorK that use PGchoicemacros.pl.
In reply to Murphy Waggoner

Re: Unrandomizing answers in pgchoicemacros.pl

by Glenn Rice -
Your original post did not state that this was a change in behavior from previous versions of webwork/pg. Had you done so, you would have received a different answer. As your question was originally worded, I gave the correct answer. A regression is something that we will investigate, and Alex has identified the change in the code that caused the issue (it turns out it was a change in the code that I made, not Perl). A hotfix pull request will be merged soon with the fix.

The PGchoicemacros.pl macro is largely considered deprecated with newer MathObject methods that replace almost all of its functionality. So as Alex noted, regressions may go unnoticed for problems that use this code. The problems in the OPL need to be updated to use the newer methods, and that is a work in progress that will take considerable time and effort. You should consider updating the problems in your local library as well.
In reply to Murphy Waggoner

Re: Unrandomizing answers in pgchoicemacros.pl

by Alex Jordan -
I think I have identified the source of this issue. First, I tried this problem in 2.19, and I saw the same behavior you see. Then I tried it in a 2.17 server I have access to, and the options are presented in a consistent order with the '?' coming first. So you might think this is about some change we made between 2.17 and 2.19.

But then I noticed the options are not just in a random order, they are in a different random order every time the page loads. This is a clue that somewhere along the way, the options are hash keys (or hash values). And with every run, the hash is ordered differently. Well, perl only started randomizing hash order with perl 5.18. So I looked at the 2.17 server I have, and sure enough its perl version is 5.16.

So I *think* that when you had the behavior that you liked before, it had to do with your WeBWorK server using an old version of perl. If you can rule that out, then maybe something else is the explanation. But that would be my working hypothesis.

Now, can we "fix" this anyway by editing the code in PGchoicemacros.pl? I am about to open a pull request to the pg repository on GitHub with something that, in testing, works to straighten out the order of the options. It will need a careful review as this will be an edit to something that could affect more than just this type of exercise.

It is worth noting that the unwanted change of behavior here comes from perl evolving to 5.18 back in 2013, and not from webwork2/pg evolving. This has probably gone unnoticed because:
  • people have a WW server with an old version of perl
  • developers who might have otherwise noticed aren't writing/using problems with PGchoicemacros.pl
  • people noticed, but just accepted the weird behavior as a quirk of an old method (like I did this past spring)


In reply to Alex Jordan

Re: Unrandomizing answers in pgchoicemacros.pl

by Alex Jordan -

Glenn has identified the real cause. At the stage where I was looking closely at WeBWorK code revision, I was only looking at PGchoicemacros.pl. But it turned out to be a change in PGbasicmacros.pl. In any case it is an issue with perl randomizing hash order, and it sounds like the PR I opened (https://github.com/openwebwork/pg/pull/1102) will address this. If you can wait for a parallel PR to be merged into the main pg branch, that is best. If you cannot wait, you could manually apply those changes.