Math.random() : [0.0, 1.0) 임의의 실수 // 0.0은 나올 수있는데 1.0은 절대 안나옴
==> 0~99사이의 난수 발생
Math.random()*100 : [0.0, 100.0)
(int)(Math.random()*100) : [0, 99] 임의의 정수
[1, 100]
(int)(Math.random()*100) + 1
[1, 6]
(int)(Math.random()*6) + 1
import java.util.Scanner;
// LAB: 난수의 합 계산하기
public class Test002 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, n, sum = 0;
System.out.print("난수의 개수: ");
n = sc.nextInt();
for(i=1; i<=n; i++) {
sum += (int)(Math.random()*100);
}
System.out.println(sum);
}
}
댓글 없음:
댓글 쓰기