--- loncom/homework/default_homework.lcpm 2002/02/06 16:18:26 1.46
+++ loncom/homework/default_homework.lcpm 2002/09/26 20:45:00 1.51
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
#
-# $Id: default_homework.lcpm,v 1.46 2002/02/06 16:18:26 albertel Exp $
+# $Id: default_homework.lcpm,v 1.51 2002/09/26 20:45:00 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -240,139 +240,171 @@ sub hinton {
sub random {
my ($start,$end,$step)=@_;
if ( ! $hidden::RANDOMINIT ) {
- srand($external::randomseed);
+ &random_set_seed_from_phrase($external::randomseed);
$hidden::RANDOMINIT=1;
}
if (!defined($step)) { $step=1; }
my $num=1+int(($end-$start)/$step);
- my $result=$start + int(rand() * $num)*$step;
+ my $result=$start + int(&math_random_uniform() * $num)*$step;
return $result;
}
sub random_normal {
my ($item_cnt,$seed,$av,$std_dev) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_normal($item_cnt,$av,$std_dev);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_beta {
my ($item_cnt,$seed,$aa,$bb) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_beta($item_cnt,$aa,$bb);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_gamma {
my ($item_cnt,$seed,$a,$r) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_gamma($item_cnt,$a,$r);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_exponential {
my ($item_cnt,$seed,$av) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_exponential($item_cnt,$av);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_poisson {
my ($item_cnt,$seed,$mu) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_poisson($item_cnt,$mu);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_chi {
my ($item_cnt,$seed,$df) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_chi_square($item_cnt,$df);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_noncentral_chi {
my ($item_cnt,$seed,$df,$nonc) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_f {
my ($item_cnt,$seed,$dfn,$dfd) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_f($item_cnt,$dfn,$dfd);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_noncentral_f {
my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_multivariate_normal {
my ($item_cnt,$seed,$mean,$covar) = @_;
+ my @oldseed=&random_get_seed();
&random_set_seed_from_phrase($seed);
@retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_multinomial {
my ($item_cnt,$seed,@p) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_multinomial($item_cnt,@p);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_permutation {
my ($seed,@inArray) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_permutation(@inArray);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_uniform {
my ($item_cnt,$seed,$low,$high) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_uniform($item_cnt,$low,$high);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_uniform_integer {
my ($item_cnt,$seed,$low,$high) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_uniform_integer($item_cnt,$low,$high);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_binomial {
my ($item_cnt,$seed,$nt,$p) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_binomial($item_cnt,$nt,$p);
+ &random_set_seed(@oldseed);
return @retArray;
}
sub random_negative_binomial {
my ($item_cnt,$seed,$ne,$p) = @_;
+ my @oldseed=&random_get_seed();
my @retArray;
&random_set_seed_from_phrase($seed);
@retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
+ &random_set_seed(@oldseed);
return @retArray;
}
@@ -442,32 +474,66 @@ sub floor {return (($_[0]-int($_[0]))==
sub format {
my ($value,$fmt)=@_;
- return sprintf('%.'.$fmt,$value);
+ my $dollarmode;
+ if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
+ my $result=sprintf('%.'.$fmt,$value);
+ $result=~s/(E[+-]*)0/$1/;
+ if ($dollarmode) {$result=&dollarmode($result);}
+ return $result;
}
sub prettyprint {
my ($value,$fmt)=@_;
my $result;
+ my $dollarmode;
+ if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
my $frac=$1;
+ if ($dollarmode) { $frac=&dollarformat($frac); }
my $exponent=$2;
$exponent=~s/^\+0*//;
- if ($external::target eq 'web') {
- $result=$frac.'×10'.$exponent.'';
- } elsif ($external::target eq 'tex') {
- $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
+ $exponent=~s/^-0*/-/;
+ if ($exponent) {
+ if ($external::target eq 'web') {
+ $result=$frac.'×10'.$exponent.'';
+ } elsif ($external::target eq 'tex') {
+ $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
+ } else {
+ $result=$value;
+ }
} else {
- $result=$value;
+ $result=$frac;
}
} else {
$result=$value;
+ if ($dollarmode) { $result=&dollarformat($result); }
}
return $result;
}
+sub dollarformat {
+ my ($number) = @_;
+ if ($number =~ /\./) {
+ while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
+ $number = $1.','.$2.$3;
+ }
+ } else {
+ while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
+ $number = $1.','.$2.$3;
+ }
+ }
+ if ($external::target eq 'tex') {
+ $number='\$'.$number; #' stupid emacs
+ } else {
+ $number='$'.$number; #' stupid emacs
+ }
+ return $number;
+}
+
sub map {
my ($phrase,$dest,$source)=@_;
+ my @oldseed=&random_get_seed();
my @seed = &random_seed_from_phrase($phrase);
&random_set_seed(@seed);
my $destct = scalar(@$dest);
@@ -479,6 +545,7 @@ sub map {
$output[$ctr] = $$dest[$idx[$ctr]];
$ctr++;
}
+ &random_set_seed(@oldseed);
return @output;
} else {
my $num = scalar(@$source);
@@ -498,10 +565,13 @@ sub map {
}
}
}
+ &random_set_seed(@seed);
+ return '';
}
sub rmap {
my ($phrase,$dest,$source)=@_;
+ my @oldseed=&random_get_seed();
my @seed = &random_seed_from_phrase($phrase);
&random_set_seed(@seed);
my $destct = scalar(@$dest);
@@ -519,6 +589,7 @@ sub rmap {
$output[$ctr] = $$dest[$r_idx[$ctr]];
$ctr++;
}
+ &random_set_seed(@oldseed);
return @output;
} else {
my $num = scalar(@$source);
@@ -544,6 +615,8 @@ sub rmap {
}
}
}
+ &random_set_seed(@oldseed);
+ return '';
}
sub capa_id { return }
@@ -655,4 +728,13 @@ sub choose {
return $_[$num];
}
+# expiremental idea
+sub proper_path {
+ my ($path)=@_;
+ if ( $external::target eq "tex" ) {
+ return '/home/httpd/html'.$path;
+ } else {
+ return $path;
+ }
+}