본문 바로가기

React-native

[ React Native ] javascript 정수범위 지정 랜덤 함수 만들기

1. Math 함수 사용

Math.random() : 0~1 의 부동소수점 난수를 생성하는 함수

Math.floor() : 소수점 1번째 자리를 버림하여 정수를 리턴하는 함수

 

2. 정수 범위 지정 랜덤 함수

min <= 랜덤숫자 <= max

const rand = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

예) const rand_int = rand(1, 10) // 1 에서 10 사이의 숫자 랜덤 발생