Skip to content

Instantly share code, notes, and snippets.

@fzed51
Created March 6, 2020 07:59
Show Gist options
  • Save fzed51/0658b49e42cdb7e9710488c27e3f1515 to your computer and use it in GitHub Desktop.
Save fzed51/0658b49e42cdb7e9710488c27e3f1515 to your computer and use it in GitHub Desktop.
random generator (⚠ Math.random)
/**
* génère un nnombre aléatoire
* @param minmax
* seul argument : limite max de nombre aléatoire
* 1er argumet : limite minimum
* @param max limite maximum
**/
const randomInt = (minmax: number, max = 0): number => {
let min: number;
if (max !== undefined) {
max = Math.max(minmax, 0);
min = Math.min(minmax, 0);
} else {
min = Math.min(minmax, max);
max = Math.max(minmax, max);
}
return min + Math.floor(Math.random() * Math.floor(max - min));
};
/**
* génère une chaine aléatoire
* @param long longueur de la chaine aléatoire
* @param alphabet enchantillon de caractère sue le quel se base la
* chaine aléatoire
**/
const randomString = (
long: number,
alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
): string => {
let str = "";
const longAlpha = alphabet.length;
while (long > 0) {
str = str + alphabet.charAt(randomInt(longAlpha));
long--;
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment