Random 클래스는 난수(의사 난수, Pseudo-random)를 생성하는 Java 표준 클래스입니다.
import java.util.Random;
“완전한 무작위가 아니라, 알고리즘 기반 난수”
| 항목 | Math.random() | Random |
|---|---|---|
| 반환 타입 | double | int, long, double 등 |
| 범위 제어 | 직접 계산 | 메서드 제공 |
| 객체화 | ❌ | ⭕ |
| 시드 제어 | ❌ | ⭕ |
👉 실무에서는 Random이 더 유연
Randomrandom=newRandom();
intn= random.nextInt();// int 전체 범위
intn2= random.nextInt(10);// 0 ~ 9
doubled= random.nextDouble();// 0.0 ~ 1.0
booleanb= random.nextBoolean();
random.nextInt(100);// 0 ~ 99
intvalue= random.nextInt(11) +10;