Thursday, April 14, 2016

JavaScript: Get Random Number Between A Given Range of 2 Numbers

Ever had that moment where you needed a random number between two given numbers, but find it hard to get exactly what you're looking for a regular and repeated basis? Quite often, if you go searching, you may see something like:

function getRandomIntInclusive(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

The previous is the most common method and can be found at @ developer.mozilla.org. The problem with this method is it's lack of involvement. For instance, what if you need a random decimal value between 1 and 2? What if you need to never include the minimum and maximum in the range? What if you need to ensure they are always a possibility?

There are countless reasons as to why this one method just won't do, which is why there are more than one methods @ developer.mozilla.org. But what you just want one simple method you can use regularly for most every possible instance you can think of?

Well, you're in the right place!

I've created a method and provided several ways of adding it to your site that makes everything about generating random numbers so much easier! - [Find @ github]

First, what it does:

If you simply need to get a random number within a given range, including the possible minimum and maximum, then simply call the method and pass the smallest number as the first parameter and the largest number as the second parameter, such as:

getRandInt(1, 2) = 1.2783833924813244

However, if you need to ensure it's ALWAYS a whole number, then simply add a third parameter of true!

getRandInt(1, 2, true) = 1

Or perhaps you need a set number of decimal places [Currently only supports up to 20]

getRandInt(1, 2, 19) = 1.1342048875680916797

Or maybe you need to ensure it's ONLY between the range and NEVER includes the min and max given, then simply reverse the parameters!

getRandInt(2, 1) = 1.3221148112756618

It's all pretty simple!


Now, for implementation ... can you say "EZ"?

Just how easy you ask? Well, I've taken the guess work out of most everything!

For a simple Global Method you can call anywhere, such as getRandInt(1, 2); simply add the following to your <head>

<script src="https://rawgit.com/JDMcKinstry/JavaScript-getRandInt/master/window.getRandInt.min.js" type="text/javascript"></script>

Or If you'd like to add it to the Math Object for a more familiar use, then include the following in your <head>

<script src="https://rawgit.com/JDMcKinstry/JavaScript-getRandInt/master/Math.getRandInt.min.js" type="text/javascript"></script>

OR[¡BONUS TIME!] If you want to use it as a jQuery method, then include the following in your <head> BUT make sure it comes after your jQuery inclusion!

<script src="https://rawgit.com/JDMcKinstry/JavaScript-getRandInt/master/jQuery.getRandInt.js" type="text/javascript"></script>

If you'd like to play around with it a bit and test it out, then check out this jsFiddle!

NOW GO FORTH, AND GET RANDOM!

1 comment:

  1. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level. Be with us to learn programming in new and creative way

    ReplyDelete