컴퓨터/Javascript
JavaScript random 랜덤
sayyesdoit
2019. 7. 11. 09:48
Math.floor(Math.random() * 100) + 1;
이렇게 하면, 1에서 100까지 나오고
Math.floor(Math.random() * 10);
이렇게 하면, 0에서 9까지 나오게 된다.
const fnRandomString = (num) => {
const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result= '';
const charactersLength = characters.length;
for (let i=0; i<num; i++) {
result += characters.charAt(Math.floor(Math.random()*charactersLength));
}
return result;
}
이렇게 하면, num 길이의 임의의 문자열을 얻을 수 있다.