Math 객체
- 수학적인 처리를 위한 속성과 매서드를 가진 객체
- Number 타입만 적용
Math.ceil(arg)
Math.ceil(0.9); // 1
Math.ceil(215.02); // 216
Math.ceil(85.78); // 86
Math.round(arg)
Math.ceil(0.9); // 1
Math.ceil(215.02); // 215
Math.ceil(85.78); // 86
Math.floor(arg)
Math.ceil(0.9); // 0
Math.ceil(215.02); // 215
Math.ceil(85.78); // 85
Math.random(arg)
- 0이상 1미만의 랜덤한 수를 반환
- 암호화적으로 안전하지 않으므로 보안과 관련된 용도로 사용은 금물
console.log(Math.random()); // 1~0 사이의 소수 반환
// 1 ~ 10 난수
let randomDouble = Math.random();
console.log(randomDouble, Math.ceil(randomDouble * 10)); // 0 < n <= 10 수 반환
console.log(randomDouble, Math.round(randomDouble * 10)); // 0 <= m <= 10 수 반환
// 0. 저장용 배열 만들기(length: 11, 각 요소의 값은 0)
const arrSaveCnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// 1. 루프 100만번 돌려
for(let i = 0; i < 1000000; i++) {
// 1-1. 랜덤수 획득 'Math.ceil(Math.random() * 10)'
const randomNum = Math.ceil(Math.random() * 10);
// 1-2. 저장용 배열에 나온 숫자를 카운트해서 저장
arrSaveCnt[randomNum]++;
};
// 2. 저장용 배열 확인
console.log(arrSaveCnt); // 랜덤으로 숫자가 생성된다.
abs.floor(arg)
- 절대값을 반환 (*절대값: 실수나 복소수가 원점으로부터 떨어진 거리)
Math.abs(1); // 1
Math.abs(0); // 0
Math.abs(-1); // 1