Random string

의미 없는 문자열 생성

by jinam yu

HTML

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
    <form class="p-4">
        <div class="form-group">
            <label>생성할 문자열 길이를 입력 후 Generate 버튼을 눌러 보세요.</label>
            <div class="input-group mb-3">
                <input type="number" class="form-control" v-model="number" />
                <div class="input-group-append">
                    <button type="button" class="btn btn-primary" @click="generate">
                        Generate
                    </button>
                </div>
            </div>
        </div>
        <div class="form-group">
            <input class="form-control" type="text" :value="output" readonly />
        </div>
    </form>
</div>

CSS

html, body {
  background-color: #f8f9fa;
}

Vue

new Vue({
    el: "#app",
    data: {
        number: 11,
        output: "",
    },
    computed: {},
    methods: {
        getRandomIntInclusive(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min + 1)) + min;
        },
        generate() {
            const characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_';
            let result = '';
            for (let i = 0; i < this.number; i++) {
                result += characters[this.getRandomIntInclusive(0, characters.length - 1)];
            }
            this.output = result;
        },
    },
    mounted() {
        this.generate();
    }
})