본문 바로가기

JABA

7. 랜덤 숫자

랜덤숫자

 

Math.random()

Math.random()는 0.0부터 1.0까지 임의의 숫자를 출력한다.

(0.0  ≤  Math.random()  <1.0)

따라서 아래와 같이 사용한다.

(int)(Math.random() * (최대값 - 최소값 + 1)) + 최소값

 

예) 1부터 100까지의 숫자는 아래와 같다.

int randomNumber = (int)(Math.random() * (100 - 1 + 1)) + 1;

 

'JABA' 카테고리의 다른 글

9. Array 배열  (0) 2023.03.24
8.주사위 문제  (0) 2023.03.21
6. While문  (0) 2023.03.21
5. Switch문  (0) 2023.03.21
4. Scanner 사용자 입력  (0) 2023.03.21