1

I am trying to understand a block of code i have how exactly it works. My code is an example of recursion and try to calculate the chance of a number as a number of a nth dice rolling outcome. My code is the following:

public static double f(int sum, int r) {

if (r== 0) { 
  if (pr(s) ){ 
      return 1; 
  }
  else 
    return 0; 
}

double rsum = 0; 

for (int i = 1; i <= 6; i++) { 
  rsum += f(sum + i, r- 1)/6.0; 
}
return rsum;
}

I struggle to understand how the recursion exactly is working and if I am doing what I am suppose to. Why sum takes values beyond six.

EDIT: in the debugging that it goes like that: f3(1,2) -> f(2,1) and then instead of f(3,0) it goes again to ev3(2,1) any idea why this is happening?

4
  • 1
    use a debugger with step-by-step debugging to see how it works and also have paper and pencil to write down Commented May 1, 2016 at 19:56
  • 1
    Maybe this would help: stackoverflow.com/questions/717725/understanding-recursion? If not, googling "understanding recursion" and working through a few top answers might be worth the effort? Commented May 1, 2016 at 20:01
  • System.out.println(ev3(0, 3)); this is what I call from main to take my result, Commented May 1, 2016 at 20:05
  • I am not understand if I pass through all three dices with that approach. Commented May 1, 2016 at 20:12

2 Answers 2

2

It seems the function's purpose is to average 6 different rolls of (1,2,3,4,5,6). It does this rollsLeft times, until it reaches the bottom. Basically if the sum for all the rolls is 3,5,6,11,13 or 17, then a value of 100 is provided, otherwise subtract 50 from the overall return value.

The recursion seems to be looking for the probability given all possible rolls.

Really, for sum=0 and rollsLeft=n the value is static meaning you could precompute this by hand, versus computing it at runtime.

Sign up to request clarification or add additional context in comments.

3 Comments

counte and counter1 are useless I put them just for quick debug.
You are also missing a final return statement for the function. You may want to double check you posted all the source.
I am not understand if I pass through all three dices with that approach.
1

Your function iterates over all possible permutations for some number of dice rolls and provides some metric for them. It's called recursively multiple times:

  • once initially (not recursively)
  • 6 times for all the possible results from the first dice roll
  • 62 times for all the possible results from the second dice roll (6 times for each of the possible results from the first dice roll)
  • ...
  • 6n times for all the possible results from the n-th dice roll

Your function evaluates some custom metric, but if you replace 100 with 1, -50 with 0, and generalize the sum == 3 || sum == 5 || sum == 7 || sum == 11 || sum == 13 || sum == 17 expression to an isPrime(sum) function, you'll be able to calculate the probability of the sum of n dice rolls being a prime number.

5 Comments

My only concern is to understand is how sum in the recursive function stands for the actual sum of the dices.
Unfortunately, I have no idea what the finally returned value you're calculating means - however I know that it doesn't calculate the probability to get a prime sum after a number of dice rolls. Like I've mentioned, to do that you need to add 1 (instead of 100) whenever you get a prime sum and 0 (instead of -50) whenever you get a non-prime sum. You can play around and compare the plots of the two approaches to see whether one is a good approximation for the other (I'd assume it's not).
The issue with 100 and 50 is because I am calculating something slighty different. However this is not my concern. My issue is to understand how the sum is work.
Oh, let me re-read your comment then. In case you are still unsure about how sum and rsum work with the recursive calls: sum is the accumulator that stores the dice sum for some permutation. So for the recursive branch where all 3 of your dice rolls are 6s, sum will be 18 when rollsLeft gets down to 0. rsum on the other hand is the metric that you want to return in the end and your function will return rsum for all non-leaf branches of the recursion tree.
The prob is when I am debugging my program sum variable doesnt take values higher than 13, So something is not going very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.