javascript
랜덤으로 로또 번호 생성하기 ( math.random() )
멈머이
2023. 10. 2. 16:41
728x90
<script>
let selected = [];
while (selected.length < 6) {
const num = parseInt(Math.random() * 45) + 1
if (selected.indexOf(num) == -1) {
selected.push(num);
selected.sort((a, b) => a - b)
}
}
console.log(selected)
</script>
우선 코드부터
자바스크립트의 Math.random 함수를 사용하여 중복되지 않은 랜덤한 숫자 6개를 뽑는 경우를 만들었다.
console.log로 찍어 브라우저에서 개발자 도구로 확인 가능하다.
다음시간에는 이 결과를 html에 출력하는 방법을 올려보겠다.
728x90