If you call rand(n) with n being an Integer, the method will return a
pseudorandom Integer r with 0 <= r < n. So the result will never be n.
If you want to include n, you have to call rand(n + 1).
In newer versions of Ruby you can also pass a range to the rand method,
which makes the whole thing more intutitive:
I am surprised nobody mentioned that code is flawed because it does
not adherer to the mentioned contract: the code will yield random
numbers outside the range given by min and max. Demonstration:
Sorry Tanaka Akira, you mentioned it - and I overlooked it.
Kind regards
robert
···
On Thu, Jun 28, 2012 at 11:05 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
I am surprised nobody mentioned that code is flawed because it does
not adherer to the mentioned contract: the code will yield random
numbers outside the range given by min and max.
Thanks for the help everyone! Going to use the range version as I'm
using Ruby 1.9.3, but it's great to understand exactly how rand works.
One more somewhat off topic question...
Just finished a Ruby class and currently reviewing, and trying to
improve my comfort with and knowledge of the language. Thinking about
starting a Ruby on Rails class in the next week or so, but not sure if I
should wait until I have a better grasp of Ruby or just forge ahead. Any
thoughts or suggestions?
Also, planning on going through all the Ruby Monk, learn Ruby the Hard
Way, and Ruby Koans. Any other suggested resources, or suggestions for
the order in which to go through the resources I listed. FYI, I also
have a copy of Chris Pine's book.
Thanks,
Emeka
Robert Klemme wrote in post #1066503:
···
On Thu, Jun 28, 2012 at 11:05 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:
I am surprised nobody mentioned that code is flawed because it does
not adherer to the mentioned contract: the code will yield random
numbers outside the range given by min and max.
Sorry Tanaka Akira, you mentioned it - and I overlooked it.
"Learn Ruby the Hard Way" probably won't help much if you've already
grasped the basics. This one is for real beginners.
-- Matma Rex
···
2012/6/28 Emeka Patrick <lists@ruby-forum.com>:
Also, planning on going through all the Ruby Monk, learn Ruby the Hard
Way, and Ruby Koans. Any other suggested resources, or suggestions for
the order in which to go through the resources I listed. FYI, I also
have a copy of Chris Pine's book.
You can do the test-first labs on your own machine, or if you like you
can play with the super-secret new "live" labs at http://testfirst.org/live (still pre-alpha and not pretty).
Also codeschool.com has some great stuff.
···
On Thu, Jun 28, 2012 at 9:06 AM, Emeka Patrick <lists@ruby-forum.com> wrote:
Also, planning on going through all the Ruby Monk, learn Ruby the Hard
Way, and Ruby Koans. Any other suggested resources, or suggestions for
the order in which to go through the resources I listed. FYI, I also
have a copy of Chris Pine's book.
Thanks for the reply. Also was looking to do some quick review to make
sure that I actually do grasp the basics. Any suggestions?
Thanks
Bartosz Dziewoński wrote in post #1066550:
···
2012/6/28 Emeka Patrick <lists@ruby-forum.com>:
Also, planning on going through all the Ruby Monk, learn Ruby the Hard
Way, and Ruby Koans. Any other suggested resources, or suggestions for
the order in which to go through the resources I listed. FYI, I also
have a copy of Chris Pine's book.
"Learn Ruby the Hard Way" probably won't help much if you've already
grasped the basics. This one is for real beginners.