DEV Community

Shaswat Raj
Shaswat Raj

Posted on

Generating Random Whole numbers in JavaScript in a specific range

Mozilla Developer Network page on this JavaScript Math Object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
Enter fullscreen mode Exit fullscreen mode

Credit & Logic Behind it :- https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range

At Last here is the concluded function

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
Enter fullscreen mode Exit fullscreen mode

And the minified form of the function

const getRandomInt = (min, max)=>~~(Math.random()*(max-min+1)+min)
Enter fullscreen mode Exit fullscreen mode

Discussion (15)

Collapse
jonrandy profile image
Jon Randy

About 40% faster:

const getRandomInt = (min, max)=>~~(Math.random()*(max-min+1)+min)
Enter fullscreen mode Exit fullscreen mode
Collapse
sh20raj profile image
Shaswat Raj Author

Can You Minify this Function also...

function getParameterByName( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Enter fullscreen mode Exit fullscreen mode
Collapse
darkwiiplayer profile image
DarkWiiPlayer • Edited on

No need, your browser already minified it for you and put it in a class called URLSearchParams 😝

Thread Thread
sh20raj profile image
Shaswat Raj Author

Ya bro it helped 💖

Collapse
neoprint3d profile image
Drew Ronsman

Never seen that way I will have to try and use it

Collapse
willaiem profile image
Damian Żygadło

This is soooo confusing. I have like zero idea, why this works, espencially the "~" operator.

When I do ~(-1), I got 0, but if I do ~~(-1), I got -1. emm.... what?

Collapse
jonrandy profile image
Jon Randy • Edited on

~ is the bitwise NOT operator, which will reverse all bits in the number - having converted it to a 32-bit signed integer first. Applying it twice resets the bits to their original state. It's a dirty trick to convert to an integer

w3schools.com/js/js_bitwise.asp

Collapse
codingjlu profile image
codingjlu

>> and | also work in flooring a number, but in negative values it rounds "down".

Thread Thread
jonrandy profile image
Jon Randy

It isn't flooring in these cases - merely removing the decimal part.

Thread Thread
codingjlu profile image
codingjlu

Oh, I forgot to mention—it behaves just like Math.floor unless the value is negative.

Collapse
darkwiiplayer profile image
DarkWiiPlayer

It's faster because it doesn't work; at least not for numbers bigger than what fits in a 32-bit integer ;)

Collapse
jonrandy profile image
Jon Randy

Poorly written spec ;)

Thread Thread
darkwiiplayer profile image
DarkWiiPlayer

The spec is wrong 😝

Collapse
sh20raj profile image
Shaswat Raj Author

Thanks bro... 👍👍
I will add this code to this Article 👍👍

Collapse
codingjlu profile image
codingjlu

The "minified" form doesn't behave the same. In negative numbers, the first function is inclusive, while the second one is exclusive.

Run both about 30 times to see what I mean, and pass the values -10, -1.