[JS] 난수 생성하기

도갱도갱·2022년 6월 27일
0

JS

목록 보기
15/15

Math.random()

  • 0~1 사이의 (1은 미포함) 부동소수점 난수를 생성한다.
var n = Math.random();
  • 소수점 1번째 자리를 버림하는 Math.floor() 함수를 사용해 정수를 리턴하도록 만들 수 있다.
var n = Math.floor(Math.random()*10) // 0 ~ 9
var n = Math.floor(Math.random()*100) // 0 ~ 99

Reference

0개의 댓글