Math.random()
- 0.0과 1.0 사이의 double값을 반환한다. ( 0.0 <= Math.random() < 1.0 )
java.util.Random // 클래스
int num2 = ra.nextInt(10)+1;
System.out.println(num2);
int num3 = ra.nextInt(16)+20;
System.out.println(num3);
boolean num4 = ra.nextBoolean();
System.out.println(num4? "1":"0");
java.lang.Math.random() // static 메소드
-import가 필요없다.
* - 0.0이상 ~ 1.0미만의 임의의 실수를 리턴 :double
정수 변환
1 ~ 10 사이의 임의의 정수 : 경우의 수10 ,최소값 1
(int)(Math.random() * 경우의 수 ) + 최소값
int com = (int)(Math.random() * 2 )+1;
int num5 =(int)(Math.random()*16)+20;
System.out.println(num5);
int num1 =(int)(Math.random()*10);
System.out.println(num1);
'Programming > Java' 카테고리의 다른 글
[java 2차원배열 정리]랜덤 정수 (0) | 2022.03.09 |
---|---|
자바 버전으로 인한 오류 (0) | 2022.03.04 |
[JAVA 정리] 연산자 (0) | 2022.02.28 |
[JAVA정리]변수,출력,scanner (0) | 2022.02.26 |
[이클립스 (eclipse) 설치] 이클립스 설치 및 기본설정 (0) | 2022.02.25 |