API - 2

조윤후·2021년 7월 8일

RandomClass

RandomClass란 난수 발생 클래스이다. 난수란 무작위숫자를 말한다.

무작위 숫자를 발생시키는 방법에는 2가지가 있는데
ex1 ) double b = Math.random();
ex2 ) Random random = new Random();
random.nextInt(100) // 예를 들어 100이면 0~99 까지 찍힌다.

위와같이 첫번째 방법 Math.randomd
두번째 방법 Random random = new Random();

사용방법

package sutdy.java.ex17_api;

public class RandomClass {

	public static void main(String[] args) {
		
        //첫번째 방법
		double b = Math.random();
		System.out.println(b);
        /////////////////////////////////
        
        //두번째 방법
        Random rd = new Random();
		System.out.println(rd.nextInt(50));
		
		for (int i = 0; i < 7; i++) {
			System.out.println(rd.nextInt(50));
		}
	}
}

결과값

0.25199319452403623
************
34
20
38
24
47
48
31
profile
공부하며 예제풀이 정리

0개의 댓글