Created
June 20, 2020 16:45
-
-
Save hohoonlee/50079bb7533665021cd3a90ea5a9caf7 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 num = '1,2,3,4,5,6,7,8,9'.split(','); | |
| const ca = 'A,B,C,C,D,E,E,F,G,H,I,J,J,K,L,M,M,N,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(','); | |
| const all = [...num, ...ca]; | |
| const getRandomInt = (min, max) => { | |
| min = Math.ceil(min); | |
| max = Math.floor(max); | |
| return Math.floor(Math.random() * (max - min)) + min; //최댓값은 제외, 최솟값은 포함 | |
| }; | |
| const run = (len=4, count=500) => { | |
| if(count > Math.pow(9, len)) { | |
| console.log('자리수가 너무 짧습니다.'); | |
| } | |
| const chars = [ca, num]; | |
| while(chars.length < len) { | |
| chars.push(all); | |
| } | |
| const list = new Set(); | |
| while(list.size < count) { | |
| const ticket = new Set(); | |
| chars.sort(()=>getRandomInt(-1,2)); | |
| chars.forEach(arr => { | |
| ticket.add(arr[getRandomInt(0,arr.length)]); | |
| }); | |
| const result = [...ticket]; | |
| while(result.length < len) { | |
| result.push(all[getRandomInt(0,all.length)]); | |
| } | |
| list.add(result.join('')); | |
| } | |
| // const o = {}; | |
| // list.forEach(v=>{ | |
| // o[v] = 'Y'; | |
| // }); | |
| const o = [...list]; | |
| console.log(JSON.stringify(o)); | |
| }; | |
| const args = process.argv.slice(2); | |
| const [len, count] = args; | |
| run(len, count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment