Frontend Programming/Javascript
random으로 값 가져오기
yoonstar*
2021. 2. 11. 21:08
0이상 1 미만에서 부동소숫점의 난수 반환하기
Math.random ( ) : 0이상 1미만의 구간에서 근사적으로 균일한 부동소숫점의 난수를 반환하며 이 값을 사용자가 원하는 범위로 변형할 수 있음
0이상 n 미만에서 부동소숫점의 난수 반환하기
Math.random ( ) * n
0이상 n미만의 정수 난수 반환
Math.floor(Math.random( )*n))
특정한 두 값 사이의 정수 난수 생성하기
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //최댓값은 제외, 최솟값은 포함
}