Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Last active June 30, 2018 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretoparisi/498f8fe3a6bd789615a7d901fc4d78c7 to your computer and use it in GitHub Desktop.
Save loretoparisi/498f8fe3a6bd789615a7d901fc4d78c7 to your computer and use it in GitHub Desktop.
Get a random number in JavaScript
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
let getRandomArbitrary => (min, max, fixed) {
fixed=fixed=10;
return (Math.random() * (max - min) + min).toFixed(fixed);
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
let getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let getRandomTwoPass = (min,max,floor) => {
var i = min, j = max, k = (max-min)+1, a = [], b = [], c, d, e, f, l = 0;
for(; i <= j; i++){ a.push(i); }
while(l < k){
c = !floor?Math.floor( Math.random()*100 ):Math.random();
d = !floor?Math.floor( Math.random()*100 ):Math.random();
b.push(c); e = c + d;
if((b.length != k) && (e < k)){ b.push(a[e]); }
l = b.length;
}
return b;
}
@loretoparisi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment