Iterated Dice Rolling
Pyth, 37 bytes
J1W%JQs[J\dQ\:dKsmhOQJ)=J+WgK*JcQ2tJ2
Try it online.
Mathematica, 95 89 80 characters
For[k=1,0<k<#,If[Print[k,d,#,": ",x=Tr[{1,#}~RandomInteger~k]];x<k/2#,k--,k++]]&
Ungolfed
For[
k = 1,
0 < k < #,
If[
Print[k, d, #, ": ", x = Tr[{1, #}~RandomInteger~k]];
x < k/2 #,
k--,
k++
]
] &
PHP, 164 121 112 113 109 bytes
Final version, I promise. Improved using Titus' suggestion:
function d($x,$y){for($i=$y;$i--;)$r+=rand(1,$x);echo$y."d$x: $r\n";$y+=$r/$y>$x/2?:-1;$y<$x&&$y?d($x,$y):0;}
EDIT: Added an extra byte for formatting. Forgot there's an IF in there that, thanks to dropping the "add/sub" text, could have been a ternary operator:
function d($x,$y){for($i=$y;$i--;)$r+=rand(1,$x);echo$y."d$x: $r\n";$r/$y>$x/2?$y++:$y--;if($y<$x&&$y)d($x,$y);}
Output now looks like:
1d6: 5
2d6: 11
3d6: 8
2d6: 11
3d6: 7
2d6: 4
1d6: 5
EDIT: Thanks to @Manatwork, saved me a lot! New and imrpoved version:
function d($x,$y){for($i=$y;$i--;)$r+=rand(1,$x);echo$y."d$x=$r\n";if($r/$y>$x/2)$y++;else$y--;if($y<$x&&$y){d($x,$y);}}
Previous entry:
function d($x,$y){for($i=0;$i<$y;$i++)($r+=rand(1,$x));$s=$y."d$x=$r, ";if($r/$y>$x/2){$y++;$s.="add";}else{$y--;$s.="sub";}echo $s."\n";if($y<$x&&$y>0){d($x,$y);}}`
Rolls separate dies, outputs this:
1d6=6, add
2d6=7, add
3d6=11, add
4d6=14, add
5d6=15, sub
4d6=15, add
5d6=18, add
And it's called thusly: d(6, 1);
Is displaying the Add
and Sub
suffix mandatory? This is unclear from your question.