Created
December 11, 2019 17:21
-
-
Save Aleksey-Danchin/13ac350015d8f06e80e92501b83951b4 to your computer and use it in GitHub Desktop.
Различные функции генерации случайного значения
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
const randomizer = { | |
alphabet: 'abcdeABCDE012345', | |
getInt (min = 0, max = 100) { | |
return min + Math.floor(Math.random() * (max - min + 1)) | |
}, | |
getFloat (min = 0, max = 100) { | |
return min + Math.random() * (max - min) | |
}, | |
getBoolean () { | |
return Math.random() > Math.random() | |
}, | |
getFrom (array = []) { | |
const index = Math.floor(Math.random() * array.length) | |
return array[index] | |
}, | |
getString (size = 5) { | |
let str = '' | |
while (str.length < size) { | |
const index = Math.floor(Math.random() * this.alphabet.length) | |
str += this.alphabet[index] | |
} | |
return str | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment