Skip to content

Instantly share code, notes, and snippets.

@DV8FromTheWorld
Created March 7, 2018 17:57
Show Gist options
  • Save DV8FromTheWorld/5a511a46aef5a4eb9f6ea18f2c8b472d to your computer and use it in GitHub Desktop.
Save DV8FromTheWorld/5a511a46aef5a4eb9f6ea18f2c8b472d to your computer and use it in GitHub Desktop.
Random-Utils.js
Math.seed = function(s) {
return function() {
s = Math.sin(s) * 10000
return s - Math.floor(s)
}
}
/**
* Get a random floating point number between `min` and `max`.
*
* @param {number} min - min number
* @param {number} max - max number
* @return {number} a random floating point number
*/
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Get a random integer between `min` and `max`.
*
* @param {number} min - min number
* @param {number} max - max number
* @return {number} a random integer
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* Get a random boolean value.
*
* @return {boolean} a random true/false
*/
function getRandomBool() {
return Math.random() >= 0.5;
}
function setSeed(seed) {
Math.random = Math.seed(seed)
}
module.exports = {
getRandomInt,
getRandomFloat,
getRandomBool,
setSeed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment