For firstTail = 1 To minHeads+1
pr = ProbOfStreak(numCoins-firstTail)
performs the recursion with the parameter reduced by one, as you have seen.
This will carry on until an entry in ProbArray is found > 0, to terminate the recursion, but all entries are set to -1 at start-up. And not changed until after that loop completes, which in turn requires each looped recursion to complete. Catch-22!
So I'm not surprised you see what you see. What I do not understand is how it is supposed to work!
The answer is "yes" because what is passed into the Sub is a value. Each (recursive or not) invocation of the Sub has its own distinct value of numCoins.
As a recursive invocation completes and returns to the calling invocation it also returns to that distinct value of numCoins. You can demonstrate this to yourself using a cut-down version of your program the does MsgBox calls, or by running under Debug.
(This would not apply if you used
B4X:
Sub ProbOfStreak(ByRef numCoins)
but then the call
B4X:
pr = ProbOfStreak(numCoins-firstTail)
becomes illegal (as I have just demonstrated to myself by trying it).)
This would mean that a workaround should not use a For-Next loop with a counter that runs from A to B, subtracted from NumCoins and passed to the recursionloop but a steady decrease by 1, a certain number of times and feed that into the loop.
I am not certain whether I could use the ByRef statement.....
This would mean that a workaround should not use a For-Next loop with a counter that runs from A to B, subtracted from NumCoins and passed to the recursionloop but a steady decrease by 1, a certain number of times and feed that into the loop.
I'm not sure that is going to make any difference. Perhaps someone else, who understands how the whole algorithm is intended to work, can suggest an effective modification.
No, I didn't intend that you should; it's not so appropriate for a recursive Sub. I mentioned it only to indcate that the default, in Basic4PPC Subs, is parameter passing by value not by reference.