JX405기_비트/Java

Day 02-6 Random 클래스

_하루살이_ 2023. 1. 15. 12:01

Random 클래스를 활용한 랜덤값 출력하기

 

1. random.nextDouble() 랜덤한 실수값 출력

 

2. random.nextInt() 랜덤한 정수값 출력

 

3. random.nextInt(100) + 40 : 40부터 140까지의 랜덤한 정수값 출력

random.nextInt() => 0부터 정수의 최대값까지가 범위

package day0110;
// Random 클래스
// Random 클래스는 우리가 랜덤숫자(=난수)가 필요할 때 사용하는 클래스이다.
import java.util.Random;
public class Ex07Random {
    public static void main(String[] args) {
        Random random = new Random();
        System.out.println(random.nextDouble());
        System.out.println(random.nextDouble());
        System.out.println(random.nextDouble());
        System.out.println(random.nextDouble());

        System.out.println(random.nextInt());
        System.out.println(random.nextInt());
        System.out.println(random.nextInt());
        System.out.println(random.nextInt());

        System.out.println(random.nextInt(100) + 40);
        System.out.println(random.nextInt(100) + 40);
        System.out.println(random.nextInt(100) + 40);
        System.out.println(random.nextInt(100) + 40);
    }
}