All right, it's time to look at a practical example of functions. In the past, we've used the random function from the math object to create a random number. That is the type of thing that you would use frequently. And therefore a great candidate for putting into a function would allow you to use that function in multiple situations and in multiple projects. So let's take a look at that I'm going to jump to sublime. So we want to create a function that will generate a random number between the two numbers that are provided and those two numbers will be provided to the function and then we'll generate the random number for it.
So I'm going to start out by creating a function expression. Variable random number I'm going to set that equal to function and then put my parameters will need to pay In a minimum number and a maximum number, so the lower end of the range and the upper end of the range. Now enter the curly braces, this is where we will enter a code and enter a semicolon first at the end of that function expression. So let's first create a variable, this will be the initial random value, set that equal to math dot random. And a second variable will set the random number to this variable. So this really should be a comma up here.
Alright, so we've got those set up. Now how do we go about getting a random number between two numbers that are provided? In the previous example, we've done random numbers between zero and 10. And therefore we multiplied it by 10. Because 10 was the range that we were looking for. That was a Pretty simple example, because of the number we started with.
We didn't have to do much to figure out what that range was and what we needed to multiply the results of math dot random by. Well, if we use that same concept, we can also figure out what our range is, no matter what two numbers are passed in, for example, between zero and 10, the range would be 10 minus zero, and therefore would be 10. We can use the same process, the maximum number minus the minute minimum number will give us our range and then we would multiply that by the results of math dot random which right now we're storing in the unit variable. So let's set Rand or variable Rand equal to the unit value times and now we need to use parentheses because we want to make sure that the that the minimum is subtracted from the maximum before we multiply it against in it.
So this must be done first therefore the parentheses. Alright, so now we've got the range. However, let's take an example here, what if the range was two and six, so we do six minus two that will leave four, we multiply the initial number by four. That gets us somewhere between zero and four. But if the we want to be between two and six, obviously we simply need to add the minimum number to the results of what what we get here. So we do plus men, and that will give us the random number.
Now let's go ahead and console. log that Okay, so that's function. Now let's test this out. Let's do random number. And I call that function. So we want to use parentheses inside the parentheses, we want to put our two arguments.
Let's use 12 and 15. Go ahead and save that. copy the file path. Open the console. There we have a random number between 12 and 15. All right, let me jump back to sublime.
Now in the past, we have turned the results into an integer using round. Well, I want to talk for just a minute about that because using round is not completely accurate. Let's look why that might be the case. So if we're using 12 and 15 as our as our lower and upper parts of the range, what are the chances using round that we could retrieve the number 12? Well, the chances are 12.01 we'll just use two decimal places here to make it simple to 12.49. That's our chances that it could be 12 but knows what the chances are, it could be 13 that would be 12.5 to 13.49 do you see the problem?
There is not as much a chance to arrive at 12 as there is 13 There's only half a number where there's a full number here. And that's the case for the upper end of the range as well. And so that's why we have a problem with round round work well when we were dealing with this before, because we simply wanted to learn some of the concepts involved with the math object. But now let's talk about how we can make this more accurate. So a better way would be to use floor or ceiling if you remember, these two methods from the math object floor automatically drops to the lowest integer, see, ceiling automatically goes to the highest integer. So if it were 12.99, it would still come out as 12.
That would give us a full range to achieve 12. However, we would never get to 15 with floor because for 14.99 would drop to 14. So the way we can Solve that is we can add one more number to what we're multiplying the initial number by. So if we do one plus max minus min, so let's use six and two as our example again. So six minus two is four. If we add one to it, that's five.
So now we have a range of zero to five. So now our range is larger than it should be. However, when we use floor when we do math dot floor, that brings it back down one number, even if it ends up when we add the 2699. It drops it down to six. And so that's how we can make it more accurate and eliminate the problem that we get with round. Okay, let me remove these examples down here at the bottom.
Let's go ahead and take a look and see how this works. Save it, jump back out, refresh again, open the console. And I'm going to dock the console at the bottom, so we can just see it, try it a few times and see it, make sure we don't get anything that's outside of the range of what we're looking for. Remember, it was 12 and 15. between those two numbers. It looks like it's working pretty well. We haven't received anything outside of that.
Okay. So quick review, we found a situation which is great for creating a function because now we can reuse this function in a number of different places. We also talked a little bit about the problem with using round in determining the random number. And we've looked at a better way to do that so that this function now becomes more accurate and would be something that we want to use frequently. Now, what if we wanted to use this function in other parts of the code and we wanted to get the number back so we can use that number for something, we just don't want to log it to the console. Well, we could put that code inside the function, but then it makes the function so it's no longer reusable, because then it's more specific to what we're trying to do in that particular bit of code.
So we need a way to be able to return the number from the function and that's what we'll look at next.