WeBWorK Problems

Issues with re-randomization

Issues with re-randomization

by Eric Stroyan -
Number of replies: 2
I am having an issue with problems re-randomizing on each access or reload of the browser page. This was not an issue in the past, having used these same problems from about 2005 to around 2011.
Basically, the problem accesses a macro to give students information about a particular element (atomic number, symbol, etc). They are then to answer some simple information about the element. What is happening now is that if a student answers incorrectly they get a new element and not the one thsy answered. If they simply refresh the page without any answer entry, they also get a new element.
Previously the student's numbers would persist as they do in most problems. I am wondering if the issue is with list_random or something else. Using ww and pg 2.12 on ubuntu 16.04.
Any help would be appreciated.

Here is the code I am referencing:
DOCUMENT(); # This should be the first executable line in the problem.
loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"isotopes.pl"
);
TEXT(&beginproblem);
$showPartialCorrectAnswers = 1;

@atomic_numbers = keys %isotopes;

$Z = list_random(@atomic_numbers);


$name = $isotopes{$Z}{name};
$symbol = $isotopes{$Z}{symbol};

for $who ( @{ $isotopes{$Z}{atomic_mass} } ) {
@atomic_mass = $who->{at_mass};
}

$A = list_random(@atomic_mass);

BEGIN_TEXT
How many protons are in a neutral atom of $name?
$BR

Number of protons = \{ans_rule(15)\} $BR

END_TEXT

$protons = $Z;

&ANS(num_cmp($protons,reltol=>1,format=>'%0.4g') );
ENDDOCUMENT();

The macro file has the basic structure:
%isotopes=( 1=>{name=>Hydrogen, symbol=>H, atomic_mass=> [{at_mass=>1}, {at_mass=>2}, ],}, 2=>{name=>Helium, symbol=>He, atomic_mass=> [{at_mass=>4}, {at_mass=>3}, ],}, 3=>{name=>Lithium, symbol=>Li, atomic_mass=> [{at_mass=>6}, {at_mass=>7}, ],}, 5=>{name=>Boron, symbol=>B, atomic_mass=> [{at_mass=>11}, {at_mass=>10}, ],},
In reply to Eric Stroyan

Re: Issues with re-randomization

by Danny Glin -
I can't find the original thread right now, but I believe I read something about the keys function in perl no longer returning the array in a consistent order.

This would explain what you are seeing. For a fixed seed, list_random would always grab the same index from the array, but the order of that array might change from page load to page load.

Try the following change to your code, and see if it fixes the problem:
@atomic_numbers = num_sort(keys %isotopes);
In reply to Danny Glin

Re: Issues with re-randomization

by Eric Stroyan -
Thanks again this seems to have worked.
I was thinking about this a bit more and recall at one point in writing PERL modules for my own sort of cgi based quizzes I needed to use Tie::IxHash to preserve ordering. I had that on my old server and not the newest one. I'm going to try adding Tie::IxHash in the near future and see if that makes any difference.
In the meantime your fix works. I'll change the problems. Thanks again.