자바(JAVA)
[자바/안드로이드] JAVA Math.random() 함수 사용하기
앱해피
2015. 6. 18. 10:57
자바(JAVA)의 Math.random() 함수는 무작위의 숫자를 반환해주는 메서드 입니다.
package arabiannight.tistory.com.java.test; public class Mathrandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { // 0 ~ 9까지 랜덤 숫자 구하기 int random = (int) (Math.random() * 10); System.out.println("" + random); } for (int j = 0; j < 10; j++) { // 1 ~ 10까지 랜덤 숫자 구하기 int random = (int) (Math.random() * 10) + 1; System.out.println("" + random); } } }
실행결과
1) 0 ~ 9까지 랜덤 : 9 0 ~ 9까지 랜덤 : 8 0 ~ 9까지 랜덤 : 0 0 ~ 9까지 랜덤 : 4 0 ~ 9까지 랜덤 : 1 0 ~ 9까지 랜덤 : 8 0 ~ 9까지 랜덤 : 3 0 ~ 9까지 랜덤 : 6 0 ~ 9까지 랜덤 : 2 0 ~ 9까지 랜덤 : 0 2) 1 ~ 10까지 랜덤 : 9 1 ~ 10까지 랜덤 : 5 1 ~ 10까지 랜덤 : 4 1 ~ 10까지 랜덤 : 2 1 ~ 10까지 랜덤 : 9 1 ~ 10까지 랜덤 : 1 1 ~ 10까지 랜덤 : 5 1 ~ 10까지 랜덤 : 1 1 ~ 10까지 랜덤 : 3 1 ~ 10까지 랜덤 : 10
감사합니다 ^_^