# Problem with recursion

• 12-15-2011, 11:02 PM
fatabass
Problem with recursion
Hi Guys,

I am trying to simulate a fake Roll Die, where it will always return 1. ( It is a die with two faces, 1 and 0 for simplicity. )

Here is my code:

import java.util.Random;

Code:

```public class Dice {         int rollDie()         {                 System.out.println("Rolling die..");                 Random randomGenerator = new Random();                 int i = randomGenerator.nextInt(2);                 System.out.println(i + " is the die!");                 if(i==0)                 {                         System.out.println("It is zero so i will roll again.");                         System.out.println(i);                         rollDie();                         i=55;                 }                 return i;                                }```
And here is a sample output:

Rolling die..
0 is the die!
It is zero so i will roll again.
0
Rolling die..
0 is the die!
It is zero so i will roll again.
0
Rolling die..
1 is the die!
55
Rolling die..
1 is the die!
1

Why am i getting this 55, can anyone help?

Thank you..
• 12-15-2011, 11:16 PM
2by4
Re: Problem with recursion
Quote:

Originally Posted by fatabass
Hi Guys,

I am trying to simulate a fake Roll Die, where it will always return 1. ( It is a die with two faces, 1 and 0 for simplicity. )

Here is my code:

import java.util.Random;

Code:

```public class Dice {         int rollDie()         {                 System.out.println("Rolling die..");                 Random randomGenerator = new Random();                 int i = randomGenerator.nextInt(2);                 System.out.println(i + " is the die!");                 if(i==0)                 {                         System.out.println("It is zero so i will roll again.");                         System.out.println(i);                         rollDie();                         i=55;                 }                 return i;                                }```
And here is a sample output:

Rolling die..
0 is the die!
It is zero so i will roll again.
0
Rolling die..
0 is the die!
It is zero so i will roll again.
0
Rolling die..
1 is the die!
55
Rolling die..
1 is the die!
1

Why am i getting this 55, can anyone help?

Thank you..

It is because you are setting i = 55

Why do you think you shouldn't get 55?
• 12-15-2011, 11:18 PM
fatabass
Re: Problem with recursion
Because there is rollDie() before i = 55. And that sets a new value to i, either 0 or 1. If it is zero, again rollDie()...
i = 55 must be unreachable, at least in my logic. :)
Obviously it is not.
• 12-15-2011, 11:23 PM
2by4
Re: Problem with recursion
Each method invocation has its own local version of i.

i is a local variable that sits on the stack. When the function returns, the stack unwinds and it's version of i is gone.

Do you intend i to be a field instead of local variable?
• 12-15-2011, 11:25 PM
fatabass
Re: Problem with recursion
No, but still...

Should return i; not always get the value from: int i = randomGenerator.nextInt(2); ?

Because at some point i will be 1 ( after several 0's perhaps, and rolling again due to if().. ) and that is the value I should be getting.
• 12-15-2011, 11:27 PM
2by4
Re: Problem with recursion
your return value is not used anywhere
• 12-15-2011, 11:29 PM
fatabass
Re: Problem with recursion
I have a System.out.println(i) in the class where I have my main method and where I call rollDie()..

Those 55 comes from System.out.println(i)
• 12-15-2011, 11:31 PM
fatabass
Re: Problem with recursion
I fixed it.

i = roll() makes it work.

Thanks.
• 12-15-2011, 11:34 PM
2by4
Re: Problem with recursion
the i in the calling method has nothing to do with the i in the called methoc. They just happen to have the same name. You have not used the return value of the method.

Review method calls and how to store their return values.

If Norm pops his head round the door, he may have a good reference.
• 12-15-2011, 11:36 PM
2by4
Re: Problem with recursion
Quote:

Originally Posted by fatabass
I fixed it.

i = roll() makes it work.

Thanks.

That's it. Nice one!

You have set the i in your calling function to the value of the i returned in your called function.
• 12-15-2011, 11:40 PM
fatabass
Re: Problem with recursion
Code:

```import java.util.Random; public class NeverZero {         public static void main(String[] args)         {                 NeverZero app = new NeverZero();                 System.out.println(app.neverZero());         }         int neverZero()         {                 Random generator = new Random();                 int i = generator.nextInt(2);                 if(i==0)                 {                         System.out.println("It is zero...");                         i = neverZero();                         return i;                 }//end if                 else                         return i;         }//end method neverZero() }// end Class```
I wrote a cleaner version.

Here is a simple output:
It is zero...
It is zero...
It is zero...
It is zero...
1

But it is still confusing to me..

Why do I need the return in the if block?
• 12-15-2011, 11:45 PM
fatabass
Re: Problem with recursion