Created
March 6, 2020 07:59
-
-
Save fzed51/0658b49e42cdb7e9710488c27e3f1515 to your computer and use it in GitHub Desktop.
random generator (⚠ Math.random)
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
/** | |
* 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