Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Created December 11, 2019 17:21
Show Gist options
  • Save Aleksey-Danchin/13ac350015d8f06e80e92501b83951b4 to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/13ac350015d8f06e80e92501b83951b4 to your computer and use it in GitHub Desktop.
Различные функции генерации случайного значения
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