Hello
I'm looking for a mathematical formula, that will give the average depth of a of a split recursion process,
where the chance of "going deeper" is constant.
(I know this is more f a mathematical than a perl question, but I need it for practical use in perl so.. ^^)
To simulate this problem I've written the following code:
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;
}
I need to be able to tell the average value returned by "dep()" (printed number), as a function of the given value of $chance.
How can I calculate (mathematical function) it when:
'$splt = 1" "$splt = 2" "$splt = 3" "$splt = 4" etc
I figured the question for the "$splt = 1" case, where the desired formula is $chance/(1-$chance).
The solution required knowledge of:
basic statistics, basic geometric series, basic integrals.
I would go by experimenting than graphing and than theorizing, but even when only examining the average of 1000000 tries each time, the result still varies too much to be considered accurate enough for this strategy.
I'm sure if I can understand the solution for "$splt = 2" I'll be able to figure the rest by myself.
Help please ^^
Thx a lot
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.