Ms'Note

MathEx-2 본문

IT/└▶Example Coding.JAVA

MathEx-2

Jelly_B 2020. 9. 7. 16:25

랜덤 값 생성하기 (응용)

/**
 * 랜덤값 생성하기
 * ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
 * Math, random() 메서드는 0~1범위를 double형의 값을 리턴하기 때문에,
 * 원하는 범위의 값을 생성하기 위해서는 일련의 공식 적용이 필요하다.
 */
public class Main03 {
    public static void main(String[] args) {
        System.out.println(Math.random());
        System.out.println(Math.random());
        System.out.println(Math03.random(1, 10));
        System.out.println(Math03.random(11, 20));
    }
    
    // 범위를 갖는 랜덤값을 생성하여 리턴하는 메서드
    public static int random(int min, int max) {
        int num = (int) ((Math.random() * (max - min + 1)) + min);
        return num;
    }
}

 

 

출력 결과

 

'IT > └▶Example Coding.JAVA' 카테고리의 다른 글

String-1  (0) 2020.09.07
MathEX-3  (0) 2020.09.07
MathEX-1  (0) 2020.09.04
OtherClassType-2  (0) 2020.08.27
OtherClassType-1  (0) 2020.08.27
Comments