Skip to content

Instantly share code, notes, and snippets.

@js2me
Created August 29, 2021 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save js2me/9564b20dae85c86a92e547b65d827985 to your computer and use it in GitHub Desktop.
Save js2me/9564b20dae85c86a92e547b65d827985 to your computer and use it in GitHub Desktop.
randoms ts
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