Pass by Reference

Pages: 12
Thanks! I think it didn't work initially for me was because i did not add in the min when i did range/2. I understand mathematically how it works when you work it out manually. But how did you know to add the min to range/2?
it makes sense really..

Let's say, Min is 50, and max is 90. (for our purposes here that is)
the Range is max - min, or 90 - 50 = 40. there are 40 numbers between 50 and 90.

Half of t hat range is 20. but we can't guess 20, cause it's not even higher than min. So we add Min to it, 20+50=70. 70 is half way between 50 and 90.

This is also why you were originally adding min to your random.. You picked a random number between 0 and range - 1, then add min to it.
Another way to think of it is that "halfway" between X and Y is equivalent to the
average of X and Y. The average of X and Y is ( X + Y ) / 2.

One can easily prove mathematically that

(X + Y)/2 == (Y - X)/2 + X

Thanks Aakanaar and jsmith for the explanation.

So actually if i did the guessVal=(x+y)/2 ---> it will also work right? ?Interesting to learn it in so many ways. I guess i kept thinking it should be more complicated for some reason. Thanks again!
Topic archived. No new replies allowed.
Pages: 12