• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random Number generation using recursive method  RSS feed

 
Ranch Hand
Posts: 50
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get different output every time I execute below piece of code. Can somebody please explain how this code flows?


Output 1--

in if block:1
Value:1

Output2--

in if block:1
after recursive:
Value:1

Output2--

in if block:1
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
after recursive:
Value:1


I was under impression that once flow enters if block, number will be returned to caller. But this is not the case. Method is getting recursively called after that. Can somebody tell me which return statement is actually getting called? Thank you.
 
Marshal
Posts: 4053
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be returned to the caller but it has to exit out of all the recursion first. Perhaps adding some console output to demonstrate how deep you go into the recursion will help?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
andrew ronchi wrote:I get different output every time I execute below piece of code.

Well if you're generating random numbers, isn't that what you want?
Can somebody please explain how this code flows?

Sure, but the code contains LOTS of mistakes, so I'm not sure what it'll get you.
Assuming you only want to know what happens after line 6 (the call to your recursive method);
1. At line 11 you create a new RNG (unnecessary).
2. At line 12 number is assigned the result of nextInt(2): ie 0 or 1.
3. At line 13, if number is > 0 (ie, 1) you display a message and return abs(1) - ie, 1 - to either your main() method (if this was the first call) or to either line 17 or 18 of your recursive method, depending on what happened previously.
4. At line 17, you call your method recursively and therefore go back to line 11 at a new level of recursion.

Basically, the method is wrong, which is why you're having so much trouble working out what it's doing. It also seems to me that it could fail with a StackOverflowError quite easily too, since it seems to be a coin toss whether it returns or calls itself again, and you need several "correct tosses" in a row for it to unravel completely.

My suggestion: Back up and explain to us what you want it to do.

Winston
 
andrew ronchi
Ranch Hand
Posts: 50
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim and Winston. It was just some part of my actual code. For better understanding, I modified it to create number between 0 and 1. I just wanted to know how this code flows and now I clearly understood with explanation and suggestion given by both of you. Thank you so much again.
I have added some statements to get it cleared,


Output:

before if:0
before recursive:
before if:1
in if block:1
after recursive:1
Value:1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!