[4] JavaScript • Math 객체 (주사위, 로또번호 발생시키기)

kangsun·2022년 8월 14일

JavaScript

목록 보기
4/9
post-thumbnail

📌 Math 객체

참조 사이트
https://www.w3schools.com/js/js_math.asp

  • Math.abs() : 절대값
    Math.abs(-3)   // 3
  • Math.max() - 최대값
    Math.max(7,5,9) // 9
  • Math.min() - 최소값
    Math.min(-7,-5,-9)	//-9
  • Math.pow() - 거듭제곱
    Math.pow(2,4)  // 2의 4승 16
  • Math.round() - 소수점 반올림
   Math.round(4.9) 	// 5
   Math.round(4.4) 	// 4
   Math.round(-4.2) //-4
  • Math.ceil() - 소수점 올림
   Math.ceil(4.9) 	// 5
   Math.ceil(4.4) 	// 5
   Math.ceil(-4.2)  //-4
   
  • Math.floor() - 소수점 버림
   Math.floor(4.9) 	// 4
   Math.floor(4.4) 	// 4
   Math.floor(-4.2)  //-5
  • Math.E - 원주율
    //2.718281828459045
  • Math.PI - 파이
    //3.141592653589793
  • Math.random() - 랜덤값, 무작위로 발생하는 수(난수)
    random값 발생 범위 : 0.0 <= r < 1
    				  실수값이 나옴
                      
    Math.random()*2		// 0.0 - 1까지 나온다.
    
    parseInt(Math.random()*2	// 정수값으로 0과 1까지 나온다.
    
    parseInt(Math.random()*4	// 0,1,2,3 까지 나온다.


문1) 주사위 1~6사이의 수만 발생시키시오.


    document.write(parseInt(Math.random()*6+1));

    // 0 1 2 3 4 5 + 1



        
문2) 로또번호 1~45 사이의 수만 발생시키시오

	document.write(parseInt(Math.random()*45+1));

profile
코딩 공부 💻

0개의 댓글