JS random

Heewon👩🏻‍💻·2024년 4월 26일

JS 내장된 함수중 Math가 있다. random은 0-1 float형태로된 숫자를 무작위로 추출한다.

Math.random();

//만약 0부터 10까지 추출하려면?
Math.random() * 10;

/*
근데 float형태이기 때문에 int로 바꿔줘야한다.
방법은 총 3가지가 있음.
1. round를 사용
Math.round(1.1); => 결과는 1. round는 반올림을 한다.
2. ceil을 사용
Math.ceil(1.01); => 결과는 2. 무조건 올림을 한다.
3. floor를 사용
Math.floor(1.9); => 결과는 1. 무조건 내름을 한다.
*/
Math.floor(Math.random());
profile
Interested in coding, meat lover, in love with dogs , enjoying everything happens in my life.

0개의 댓글