# Interesting irb experiment

J=126*rand+33.round
K=j.chr
Works as far as it goes, however
J=126*rand+33.round.chr
Throws an error. The actual error message seems strange after .round
did its work.

DaShiell, Jude T. CIV NAVAIR 1490, 1, 26 wrote:

J=126*rand+33.round
K=j.chr
Works as far as it goes, however
J=126*rand+33.round.chr
Throws an error. The actual error message seems strange after .round
did its work.

You're not showing actual copy-pasted code nor error message, but at a
guess I'd say you need something like

j=((126*rand+33).round.chr

Using rand(126) would avoid the need for round

···

--
Posted via http://www.ruby-forum.com/\.

I don't know what you want to obtain. If you want the character with code
126*rand + 33 (rounded), then you need to put some parentheses. I think there
are two issues with your code. One is the direct cause of the error you see
(by the way, it would have been better to post it, rather than only saying
it's strange): the chr method is called on the value returned by 33.round,
returning "!". So, your code evaluates to:

J=126*rand + "!"

Since you can't add a string to a number, you get the error. To obtain the
correct result, you should put everything from 126 to round in parentheses:

J=(126*rand + 33.round).chr

The second issue regards 33.round. Since 33 is an integer, 33.round will still
give you 33. What I think you wanted is to call round on the result of the
sum. It's the same kind of mistake I mentioned previously, and it's solved in
the same way, by putting parentheses:

J=(126*rand + 33).round.chr

By the way, note that you can generate a random integer between 0 and n by
calling rand(n), so your code coud be rewritten more simply as:

J=(rand(126) + 33).chr

I hope this helps

Stefano

···

On Wednesday 06 October 2010, DaShiell, Jude T. CIV NAVAIR 1490, 1, 26 wrote:

>J=126*rand+33.round
>K=j.chr
>Works as far as it goes, however
>J=126*rand+33.round.chr
>Throws an error. The actual error message seems strange after .round
>did its work.