interesting irb experiment

  • Thread starter DaShiell, Jude T. CIV NAVAIR 1490, 1, 26
  • Start date
D

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

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

Brian Candler

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
 
S

Stefano Crocco

|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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top