--- loncom/homework/default_homework.lcpm 2001/06/12 21:07:32 1.26 +++ loncom/homework/default_homework.lcpm 2001/06/13 20:00:07 1.27 @@ -358,8 +358,9 @@ sub sub_string { } sub pow {return $_[0] ** $_[1]; } -sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); } -sub floor {return int($_[0]); } +sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); } +sub floor {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); } +#sub floor {return int($_[0]); } sub format { my ($value,$fmt)=@_; @@ -367,21 +368,57 @@ sub format { } sub map { - my $num = $#_/2; - my $i; - my @used; - my @permutation; - for ($i=0; $i < $num;) { - $rand = &random(1,$num,1); - if ( $used[$rand] == '0' ) { - $i++; - $used[$rand]='1'; - $permutation[$i]=$rand; + my ($phrase,$dest,$source)=@_; + my @seed = &random_seed_from_phrase($phrase); + &random_set_seed(@seed); + my $num = scalar(@$source); + my $destct = scalar(@$dest); +# print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num; + my @idx = &math_random_permuted_index($num); + my $ctr = 0; + my $tot = $num; + $tot = $destct if $destct < $num; + if (ref($$dest[0])) { + while ($ctr < $tot) { + ${$$dest[$ctr]} = $$source[$idx[$ctr]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$idx[$ctr]]; + $ctr++; + } + } +} + +sub rmap { + my ($phrase,$dest,$source)=@_; + my @seed = &random_seed_from_phrase($phrase); + &random_set_seed(@seed); + my $num = scalar(@$source); + my $destct = scalar(@$dest); +# print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num; + my @idx = &math_random_permuted_index($num); + my $ctr = 0; + my $tot = $num; + $tot = $destct if $destct < $num; + my @r_idx; + while ($ctr < $tot) { + $r_idx[$idx[$ctr]] = $ctr; + $ctr++; + } + $ctr = 0; + if (ref($$dest[0])) { + while ($ctr < $tot) { + ${$$dest[$ctr]} = $$source[$r_idx[$ctr]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$r_idx[$ctr]]; + $ctr++; + } } - } - for ( $i=1 ; $i<$num+1 ; $i++ ) { - ${$_[$permutation[$i]]}=$_[$i+$num] - } } sub capa_id { return }