UNPKG

get-random-float

Version:

Gets random float

85 lines (67 loc) 2.04 kB
/* lib */ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } function getRandomFloat(min, max) { if (typeof min === "string") { min = Number(min); } if (typeof max === "string") { max = Number(max); } let result; if (min === undefined) { max = getRandomInt(0, 1_000_000); min = getRandomInt(-1_000_000, 0); result = Math.random() * (max - min) + min; // makes the number smaller if (getRandomInt(0, 1) === 1) { result = result / getRandomInt(10_000, 100_000); } // makes the number bigger if (getRandomInt(0, 1) === 1) { result = result * getRandomInt(-100, 1000); } // sometimes makes the number smaller, sometimes bigger if (getRandomInt(0, 1) === 1) { result = result / getRandomInt(-100, 10); } let decimal = getRandomInt(1, 10); result = result.toFixed(decimal); result = Number.parseFloat(result); if (result % 1 === 0) { result += Math.random() * 0, 9 + 0, 1; } } else { result = Math.random() * (max - min) + min; } return result; } function likeFloat3(inputValue) { if (typeof inputValue !== "string" && typeof inputValue !== "number") { return false; } inputValue = String(inputValue); let regExpFloat = /^-?\d*\.\d+$/; let result = inputValue.search(regExpFloat) === -1 ? false : true; return result; } function isFloat(number) { if (number % 1 > 0 || number % 1 < 0) { return true; } else { return false; } } function isDirectChild(object, functionConstructor) { return functionConstructor.prototype === Object.getPrototypeOf(object); } module.exports = { getRandomInt, getRandomFloat, likeFloat3, isFloat, isDirectChild };