[원문] - http://mwultong.blogspot.com/2006/11/java-mathrandom-int.html
Random 클래스 없이, 간단히 정수 난수 구하기 예제
public class Foo {
public static void main(String[] args) {
// 0.0 ~ 1.0 사이의 실수 난수 구하기
for (int i = 1; i <= 20; i++)
System.out.println(Math.random());
/* 출력 결과:
0.8835488755737285
0.7442235907969202
0.04143887519495115
0.7752670113987891
0.525644276817284
0.9810655979902362
0.9857864655525691
0.5176456441171947
0.9534154184106848
0.5711598917262706
0.9167881479510426
0.9683895991289863
0.5570482364156645
0.640266931881892
0.7521635329694171
0.37944742406283405
0.1930044225804538
0.6713601888567906
0.28656122448550325
0.9697966362643208
*/
// 1 ~ 10 까지의 정수 난수 구하기
for (int i = 1; i <= 20; i++) {
int n = (int) (Math.random() * 10) + 1;
System.out.println(n);
}
/* 출력 결과:
8
4
2
2
7
1
3
7
10
3
7
2
10
8
3
5
4
10
4
1
*/
}
}
- Posted
- Filed under JSP, JAVA