<In Story>

자바스크립트 정수 난수만들기, Javascript Random(integer) // [Programming] [Script] [Web] [JavaScript] 본문

Programming/Web

자바스크립트 정수 난수만들기, Javascript Random(integer) // [Programming] [Script] [Web] [JavaScript]

<In Story, Hi story, History> 2015. 10. 4. 19:12

http://rankingis.com/?titlequery=자바스크립트-난수-랜덤-숫자-만들기-예제

http://mwultong.blogspot.com/2006/11/random-javascript.html


Math.floor(Math.random() * 100) + 1;

이렇게 하면, 1에서 100까지 나오고


Math.floor(Math.random() * 10);

이렇게 하면, 0에서 9까지 나오게 됩니다.


그럼 100에서 200까지의 난수를 구하려면 어떻게 할까요?

위 예제와 거의 같으나 최소값은 100이고 최대값은 200이 되겠죠?

function randomRange(n1, n2) {

return Math.floor(Math.random() * (n2 - n1 + 1)) + n1;

// Math.floor(Math.random()*(101)) + 100;

// Math.floor(Math.random()*(최대값-최소값+1)) + 최소값;

}