It came down to a difference in how the perlrand function and the WeBWorK random function treated the right limit. When (from the original perl subroutine) I translated the line
$j = int(rand(0,$i+1)); #random number between 0 and $i
$j = random(0,$i+1); #random number between 0 and $i+1
this occasionally (when $i+1 was selected) and would shuffle an empty value onto the array.