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?

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

2 Answers2

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.

ArcSine
  • 648
  • 6
  • 14
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.

Dimitar Dimitrov
  • 16,032
  • 5
  • 53
  • 55
  • My only concern is to understand is how sum in the recursive function stands for the actual sum of the dices. – konstantin May 01 '16 at 21:28
  • 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). – Dimitar Dimitrov May 01 '16 at 21:46
  • 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. – konstantin May 01 '16 at 21:49
  • 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. – Dimitar Dimitrov May 02 '16 at 06:59
  • The prob is when I am debugging my program sum variable doesnt take values higher than 13, So something is not going very well. – konstantin May 02 '16 at 07:01