From: Sam H. v. a. <act...@de...> - 2004-12-16 02:40:26
|
Log Message: ----------- added fisher_yates_shuffle() inplace-randomization routine. Modified Files: -------------- webwork2/lib/WeBWorK: Utils.pm Revision Data ------------- Index: Utils.pm =================================================================== RCS file: /webwork/cvs/system/webwork2/lib/WeBWorK/Utils.pm,v retrieving revision 1.59 retrieving revision 1.60 diff -Llib/WeBWorK/Utils.pm -Llib/WeBWorK/Utils.pm -u -r1.59 -r1.60 --- lib/WeBWorK/Utils.pm +++ lib/WeBWorK/Utils.pm @@ -74,6 +74,7 @@ cryptPassword dequote undefstr + fisher_yates_shuffle sortByName ); @@ -721,6 +722,18 @@ sub undefstr($@) { map { defined $_ ? $_ : $_[0] } @_[1..$#_]; +} + +# shuffle an array in place +# Perl Cookbook, Recipe 4.17. Randomizing an Array +sub fisher_yates_shuffle { + my $array = shift; + my $i; + for ($i = @$array; --$i; ) { + my $j = int rand ($i+1); + next if $i == $j; + @$array[$i,$j] = @$array[$j,$i]; + } } ################################################################################ |