01. Math.random()

Math.random()
  • 0~1 사이의 난수 값을 변환합니다.
번호 기본값 메서드 리턴값 클릭
document.querySelector(".sample01_N1").innerHTML = "1";
document.querySelector(".sample01_Q1").innerHTML = "0~1(난수)";
document.querySelector(".sample01_M1").innerHTML = "Math.random()";
document.querySelector(".sample01_B1").addEventListener("click", () => {
    document.querySelector(".sample01_P1").innerHTML = Math.random();
});

document.querySelector(".sample01_N2").innerHTML = "2";
document.querySelector(".sample01_Q2").innerHTML = "0~9(정수)";
document.querySelector(".sample01_M2").innerHTML = "Math.random()";
document.querySelector(".sample01_B2").addEventListener("click", () => {
    document.querySelector(".sample01_P2").innerHTML = Math.floor(Math.random()*10);
});

document.querySelector(".sample01_N3").innerHTML = "3";
document.querySelector(".sample01_Q3").innerHTML = "1~100(정수)";
document.querySelector(".sample01_M3").innerHTML = "Math.random()";
document.querySelector(".sample01_B3").addEventListener("click", () => {
    document.querySelector(".sample01_P3").innerHTML = Math.floor((Math.random()*100) + 1);
});