<!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>random.html</title>
    </head>
    <body>
    	<input type="button" value="랜덤수 생성" onClick="makeRandom();"><br>
    	<input type="number" id="target">
    
    	<script>
    		function makeRandom() {
    			var num = Math.random(); // 0 <= num < 1
    			// 0 ~ 9 조정 : parseInt(num * 10) -> 정수로 변환
    			// 1 ~ 10 조정 : parseInt(num * 10) + 1
    			// 1 ~ 6 조정 : parseInt(num * 6) + 1
    			// 랜덤수 구하는 공식
    			// -> parseInt(Math.random() * 갯수) + 시작값
    			// 501 ~ 1000 사이의 임의의 수
    			// ->parseInt(Math.random() * 500) + 501
    			document.getElementById("target").value = parseInt(num * 500) + 501;	
    		}
    	</script>
    </body>
    </html>

    'JavaScript > Vanilla JavaScript' 카테고리의 다른 글

    입력한 수의 합계 구하기  (0) 2023.09.11
    가위바위보 게임 구현  (0) 2023.09.11
    간단한 inline Script  (0) 2023.09.11

    댓글