Skip to content

Instantly share code, notes, and snippets.

@Leko
Created March 15, 2016 03:32
Show Gist options
  • Save Leko/e9131a436c88ebd4dcd4 to your computer and use it in GitHub Desktop.
Save Leko/e9131a436c88ebd4dcd4 to your computer and use it in GitHub Desktop.
シリアル番号っぽいものをランダム生成する
'use strict'
const shuffle = (list) => {
let i, j, temp
if (typeof list === 'string') {
list = list.split('')
} else if(Array.isArray(list)) {
list = list.slice()
}
i = list.length
if (i === 0) {
return list
}
while (--i) {
j = Math.floor(Math.random() * (i + 1))
temp = list[i]
list[i] = list[j]
list[j] = temp
}
return list
}
const serialize = (len = 16, prefix = '') => {
// I, 1, 0, Oなど勘違いしやすい文字を除外
const seed = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
return prefix + shuffle(seed).slice(0, len - prefix.length).join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment