filmov
tv
JavaScript Random Number Generator In 90 Seconds #JavaScriptJanuary
![preview_player](https://i.ytimg.com/vi/Q3PBJnI6QKg/maxresdefault.jpg)
Показать описание
Watch the entire #JavaScriptJanuary series from the beginning!
What if we wanted to get a random number between 1 - 10?
We can easily do this by multiplying the random number by the maximum range that we are looking for. In this case, 10.
But, this is actually going to give us a random number between 0 - 9. So in order to get a number between 1 - 10 we will need to add 1 at the end.
Note that this solution will only work for ranges between 1 and whatever number you multiply by.
So how do we get a range of random numbers between let’s say, 5 and 15?
This is going to require a bit more math. We’ll create an arrow function which accepts two values and we’re basically going to do the same thing but instead of multiplying by an arbitrary number, we are going to subtract the maximum and minimum values then add one, and then at the very end we will also add the minimum value. If you hate math, just don’t think about it. It works :)
Next we’ll call the function and pass our minimum and maximum values. Now every time we refresh we get random numbers between the range we defined.
I'll be releasing a new video every day in January! Stay tuned..
_____________________________________
📚 Learn to CODE in just a FEW months here:
_____________________________________
🛠️ Tools I use:
_____________________________________
💖 Show support!
_____________________________________
Watch Next:
_____________________________________
Connect With Me:
_____________________________________
** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.
#JavaScriptJanuary #31Days31Videos #codeSTACKr
What if we wanted to get a random number between 1 - 10?
We can easily do this by multiplying the random number by the maximum range that we are looking for. In this case, 10.
But, this is actually going to give us a random number between 0 - 9. So in order to get a number between 1 - 10 we will need to add 1 at the end.
Note that this solution will only work for ranges between 1 and whatever number you multiply by.
So how do we get a range of random numbers between let’s say, 5 and 15?
This is going to require a bit more math. We’ll create an arrow function which accepts two values and we’re basically going to do the same thing but instead of multiplying by an arbitrary number, we are going to subtract the maximum and minimum values then add one, and then at the very end we will also add the minimum value. If you hate math, just don’t think about it. It works :)
Next we’ll call the function and pass our minimum and maximum values. Now every time we refresh we get random numbers between the range we defined.
I'll be releasing a new video every day in January! Stay tuned..
_____________________________________
📚 Learn to CODE in just a FEW months here:
_____________________________________
🛠️ Tools I use:
_____________________________________
💖 Show support!
_____________________________________
Watch Next:
_____________________________________
Connect With Me:
_____________________________________
** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.
#JavaScriptJanuary #31Days31Videos #codeSTACKr
Комментарии