palkia has asked for the wisdom of the Perl Monks concerning the following question:
I need to be able to tell the average value returned by "dep()" (printed number), as a function of the given value of $chance.print 'num of splits [positive integer]: '; my $splt = <>; print 'chance of going deeper [between 0 and 1]: '; my $chance = <>; my $avgOf = 1000000; my $sum = 0; for(my $n=0;$n<$avgOf:$n++) {$sum += dep();} print "avg depth is ".($sum / $avgOf)."\n"; exit; sub dep { if(rand() < $chance) {return 1+max(multiDep())}else {return 0;} } sub multiDep { my @keep; for($i=0;$i<$splt;$i++) {$keep[$i] = dep();} return @keep; } sub max { my $hi = shift; for my $v (@_) {if($v > $hi){$hi = $v;}} return $hi; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Average random depth formula required
by SuicideJunkie (Vicar) on May 01, 2012 at 21:35 UTC | |
by palkia (Monk) on May 01, 2012 at 22:43 UTC | |
by SuicideJunkie (Vicar) on May 02, 2012 at 13:22 UTC | |
by palkia (Monk) on May 05, 2012 at 16:12 UTC |