On Sun, Nov 27, 2016 at 3:06 PM, Micky Scandal <mickyscandal / gmail.com> wrote:

> if I do something like this:
> num = rand(10)
> 5.times {puts num}
> that will give me the same random number 5 times (ie. 2 2 2 2 2). but if I
> do the same thing but don't store the random number in a variable:
> 5.times {puts rand(10)}
> then it works as expected (2 6 1 1 9 4). does anyone know why that is?

It's doing exactly what you told it to do, not what you *meant*.
That's a very annoying habit computers often seem to have.  ;-)

Computer programming is the art of giving precise instructions to a
very fast and very literal-minded *idiot*, who has absolutely zero
skills about interpreting what you really meant.  So, to understand
what's going on, pretend that instead of writing Ruby to a computer,
you are speaking to a person.  He's a very stupid person who only
understands the very simplest instructions.  However, he can do things
very quickly, he will do exactly as instructed (if at all possible),
and if you give a *name* to a set of instructions, he can remember
what instructions that name referred to, so that you can reference
that name in later instructions.

So the first conversation is like this:

  YOU: Write down the word "num" on a piece of paper.

  HIM: Okay.  {writes word}

  YOU: Roll this ten-sided die, and whatever number comes up, write
that underneath the word "num" on that piece of paper.

  HIM: Okay.  {rolls die, it comes up 2, he writes 2 under "num"}

  YOU: When I say "execute block", what I mean is, that number you
just wrote underneath "num", write that on the next empty line on this
other piece of paper.

  HIM: Okay.  {remembers what "execute block" means -- but doesn't *do* it yet}

  YOU: Execute block, five times.

  HIM: Okay.  {writes 2 five times on the second piece of paper}

While what you *really* wanted, and the second chunk of code would
represent, is:

  YOU: When I say "execute block", what I mean is, roll this ten-sided
die, and whatever number comes up, write that on the next empty line
on this piece of paper.

  HIM: Okay.  {remembers what "execute block" means -- but doesn't *do* it yet}

  YOU: Execute block, five times.

  HIM: Okay.  {five times, he rolls the die and writes whatever number
comes up.}

It's not a matter of storing anything in a variable, it's a matter of
rolling the die again.

> and
> if I did want to store a random value in a variable, how would i then do so
> without getting the same number every time I call it?

It sounds to me like either you're not quite understanding what "store
a value in a variable" means, or I'm not understanding your question.
Once you store a value in a variable, the value does not change unless
you store another value in that same variable.  (Barring of course
random accidents like bit-flips caused by hardware interference, some
other thread that has access to that memory changing it, etc.)  That's
the main reason we use variables, to store some value we just
calculated (or got as input, or whatever), so we can access that same
value again.

On the other claw, there is a way that "5.times {puts num}" can give
you five (probably) different random value.  You could make "num" not
a variable, but a method that returns rand(10).  So, when the "puts
num" block is executed, it isn't puts'ing the value of a variable, but
of a method call.  Does that help?

For now, I'll leave you to ponder this classic comic strip:

https://xkcd.com/221/

-- 
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>