Search

조건문1_개념01_랜덤

대분류
STEP03 조건문/일차반복문
문제 난이도
필수
소분류
조건문1_개념

영상

개념

<script> // 0 < Math.random() < 1 let num = Math.random(); document.write("Math.random() = " + num + "<br>"); document.write("Math.random() * 3 = " + num * 3 + "<br>"); document.write("Math.floor(Math.rnadom() * 3) = " + Math.floor(num * 3)); /* 0.0xx 0.0xx 0 0.1xx 0.3xx 0 0.2xx 0.6xx 0 0.3xx 0.9xx 0 0.4xx * 3 1.2xx Math.floor() 1 0.5xx 1.5xx 1 0.6xx 1.8xx 1 0.7xx 2.1xx 2 0.8xx 2.4xx 2 0.9xx 2.7xx 2 */ </script>
JavaScript
복사
<script> // 랜덤 let rand = Math.random(); // 0 < 소수점 < 1 console.log(rand); document.write(rand + "<br>"); // 만약에 0~2사이의 값을 뽑는다면, rand = rand * 3; console.log(rand); document.write(rand + "<br>"); rand = Math.floor(rand); // 0부터 3개 0, 1, 2 console.log(rand); document.write(rand + "<br>"); rand = Math.floor(Math.random() * 3); console.log(rand); document.write(rand + "<br>"); console.log("-------------------------"); document.write("-------------------------<br>"); // 예) 1~6 주사위 rand = Math.floor(Math.random() * 6) + 1; // [0 ~ 5] + 1 console.log("결과1 = " + rand); document.write("결과1 = " + rand + "<br>"); // 예) -3 ~ 3 사이의 랜덤값 rand = Math.floor(Math.random() * 7) - 3; // [0 ~ 6] - 3 console.log("결과2 = " + rand); document.write("결과2 = " + rand); </script>
Java
복사