Last active
June 30, 2018 11:44
-
-
Save loretoparisi/498f8fe3a6bd789615a7d901fc4d78c7 to your computer and use it in GitHub Desktop.
Get a random number in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Array shuffle see https://gist.github.com/loretoparisi/c09e131d0b731c64fff498b288eba78e