In this lesson
we'll learn about random numbers in Ruby.
So let's begin with the built in rand method. rand,
what this method does is, it generates a
random floating point number between 0 and 1.
So for example, here
I can invoke my puts method and immediately invoke the random method right after it.
It's another built in top level method available in Ruby
and it will generate a random number.
So you can see on the right hand side,
I get a random floating point number between 0 and 1,
and I'm just going to run the file a couple of times.
So you see that the result is different each time.
Of course,
we get a pretty complex floating point number with a lot of digits after the decimal.
But given that this is a floating point number,
there is a round method available on it which can
allow you to round to a certain level of precision,
and precision simply refers to the number of digits after the decimal point.
So for example, right here
I can invoke rand, and once I get that floating point object back, the float
I can call the round method on it,
and round to for example two digits.
And that makes it a lot easier to read.
Here's another example with me rounding to a precision of 4,
and there we can see our results.
So this is helpful. But of course, it only gives us a number between 0 and 1.
So if we want a floating point value that is greater,
we can simply multiply the result here by that number.
For example, I can say something like take
the rand method,
give me a random floating point value rounded to
two digits, and then multiply it by 30.
And that will give me a random floating point number between 0 and 30.
So there's 2.1, and there's 18.0, and there's 27.0,
and 9.3, etcetera, right?
So that is how we generate
a random floating point number.
We can also ask Ruby to generate a random integer.
So the first option that we have available to us
is to simply pass rand a number.
When we do this, rand is going to give us an integer between 0
and the number we pass here.
It's important to note that this number that we
pass here is going to be the upper bound,
but it's also going to be exclusive,
which means Ruby is going to go up to that
number but not actually included in the possible results.
So for example, if I provide a value of 100,
what that will give us is an integer between 0 and 99 inclusive, or 100 exclusive.
So you're never actually going to get 100 here. It's going to be between 0 and 99.
So the upper bound here or 100 in other words,
is not included.
So if you wanted a random number between 0 and 100
you have to put in a value that is one greater.
So for example, if I invoke rand with an argument of 101,
that allows us to get 100 at some point in the result set. All right.
And just to prove this to you, what I can do is invoke rand with an argument of 1.
So because the rand method with an argument
gives you an integer between 0 and 1,
and because the upper bound is exclusive,
there's only one option that Ruby can return here because
it can't return 1, and that's going to be 0.
So it's not actually going to be random, you're always going to get 0.
But that is proving the point that this number is exclusive.
It represents the number to go up to, but it's not actually going to be a valid option.
So that's something you have to watch out for.
All right.
So that's how you can generate random floating point numbers as well as
a random whole number between 0 and some point.
But there's one other scenario that's missing.
And that is how do we generate
a random number in a range? For example, what if I want a random number between 50 and 60?
Right? Maybe I'm building a card program or some kind of game,
and I need a random number generator.
Well, what we can do is we can pass the random method
a range object, and that range object will represent the range
of values from which the random one will be chosen.
And once again, the idea of an inclusive versus exclusive range
matters.
So if I use two dots, that means inclusive, which means both values will be included.
So
what I'm asking Ruby for here is a random
number between 1 and 100 and 100 will be included.
So you're gonna see we're gonna get 95, and 34, then 92.
Here's another example, I can do 50 to 60,
and we're always going to get a value in between
those
two values.
You'll notice we're gonna get 60 here,
which is proving that that upper bound of 60 is included.
We can include three dots.
It's going to operate the exact way you expect on a range,
which means it's going to make the upper bound exclusive or excluded.
So something like this will never result in a value of 60.
We're going to go from 50 up to 60 but never actually include
uh 60.
So same rules apply two dots means include the
final value three dots means exclude the final value.
But by using a range in combination with the RAND method, we can generate a integer
in between a starting point and an ending point.
And that is just a quick introduction to how you can generate random numbers in Ruby.
Super helpful for certain situations. All right,
that's all there is to cover in this lesson. So I will see you in the next one.