1. 지정 범위까지 난수 발생 하기
public class Example {public static void main(String[] args) {
// 3에서 12까지 범위의 정수를 랜덤하게 20개 출력
for (int i = 1; i <= 20; i++)
System.out.println(randomRange(3, 12));
}
// 지정된 범위의 정수 1개를 램덤하게 반환하는 메서드
// n1 은 "하한값", n2 는 상한값
public static int randomRange(int n1, int n2) {
return (int) (Math.random() * (n2 - n1 + 1)) + n1;
}
}
댓글
댓글 쓰기