Created
August 29, 2021 22:43
-
-
Save js2me/9564b20dae85c86a92e547b65d827985 to your computer and use it in GitHub Desktop.
randoms ts
This file contains hidden or 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
import * as getRandomWord from "random-words"; | |
export const getRandomFloat = <T extends number = number>(min = 0, max = 1): T => { | |
return (Math.random() * (max - min) + min) as T; | |
}; | |
export const getRandomInt = <T extends number = number>(min = 0, max = 1): T => { | |
if (min === max) return min as T; | |
return Math.round(getRandomFloat(min, max)) as T; | |
}; | |
export const getRandomChoice = <O extends unknown>( | |
...choices: ((choiceIndex: number) => O)[] | |
): O => { | |
const randomChoice = getRandomInt(0, choices.length - 1); | |
return choices[randomChoice](randomChoice); | |
}; | |
export const getRandomBunchOfWords = () => { | |
return `${getRandomWord()}_${getRandomWord()}`; | |
}; | |
export const getRandomSizeArray = (min = 0, max = 10) => Array(getRandomInt(min, max)).fill(null); | |
export const getMajorRandomBool = () => { | |
return getRandomInt(0, 10) <= 6; | |
}; | |
export const getMinorRandomBool = () => { | |
return !getMajorRandomBool(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment