Archives
Recent Posts
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
관리 메뉴

개발도생's Blog

[Java][Class] Random 본문

Java/Class

[Java][Class] Random

개발도생 2023. 3. 20. 16:00

난수를 생성할 수 있는 Random Class에 대해서 공부해 봤다.

 

Random Class는,

java.util Package에 속해 있는 Class다.

 

Radnom Class는 Math Class의 'random' Method와 마찬가지로 난수 값을 생성한다.

 

import로 Package를 불러와서 Random Class 객체를 생성하고 인스턴스를 활용해 사용해 주면 된다.

 


Radnom Class 사용법

import로 Package를 불러와서 Random Class 객체를 생성하고 인스턴스를 활용해 사용해 주면 된다.

import java.util.Random;

public class Random {
	
    public static void main(String[] args) {
    	Random random = new Random();
    }
}

 

Radnom Class의 Method

Random Class에서 제공하는 Method를 통해 원하는 자료형의 난수를 생성할 수 있다.

nextBoolean() boolean형 난수 반환
범위 : true or false
nextInt()  int형 난수 반환 
범위 : 2,147,483,648 ~ 2,147,483,647
 nextLong()   long형 난수 반환
범위 : -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
 nextFloat()  float형 난수 반환 
범위 :  0.0이상 1.0미만
 nextDouble()  double형 난수 반환 
범위 :  0.0이상 1.0미만

 

Radnom Class의 Method 활

import java.util.Random;

public class Random {

	public static void main(String[] args) {
		
		Random random = new Random();
		
		/*
		 *  boolean 난수 생성
		 *   true or false 반환
		 */
		System.out.println("boolean형 난수 값 : " + random.nextBoolean());

		System.out.println("----------------");
		/*
		 *  int 난수 생성
		 *   -2,147,483,648 ~ 2,147,483,647 반환
		 *    0이상 n미만의 범위내에 있는 int형 난수 반환 
		 */
		System.out.println("int형 난수 값 : " + random.nextInt());
		
		System.out.println("----------------");
		/*
		 *  long 난수 생성
		 *   -2,147,483,648 ~ 2,147,483,647 반환
		 */
		System.out.println("long형 난수 값 : " + random.nextLong());

		System.out.println("----------------");
		/*
		 *  float 난수 생성
		 *   0.0이상 1.0미만 반환
		 */
		System.out.println("float형 난수 값 : " + random.nextFloat());
		
		System.out.println("----------------");
		/*
		 *  double 난수 생성
		 *   0.0이상 1.0미만 반환
		 */
		System.out.println("double형 난수 값 : " + random.nextDouble());
	}
}

 


Random Class의 Method를 활용할 때, 

 

정수 타입의 자료형 'int'의 난수 반환 범위를 지정해 줄 수 있다.

import java.util.Random;

public class Random {

	public static void main(String[] args) {
		
		Random random = new Random();
		
		/*
		 *  nextInt(int N) - 범위 지정
		 *   범위 : 0이상 N미만
		 *    
		 */
		System.out.println("int형 난수 값 : " + random.nextInt(10));
	}
}

 nextInt() Method에 매개변수를 정수 값으로 입력해 주면 난수 생성 범위를 지정해 줄 수 있다.

 

위의 코드와 같이 매개변수를 정수 값 10으로 입력해 주었을 때,

 

범위가 0 ~ 10 미만의 숫자 중 무작위로 반환된다.

 

Random Class의 nextInt(int N) Method는 범위가 정수 '0'부터 시작이고,

 

입력된 값은 범위에 포함되지 않는다.

 

하지만, 0부터 시작을 원하지 않을 경우에는 범위를 조정해 줄 수 있다.

import java.util.Random;

public class Random {

	public static void main(String[] args) {
		
		Random random = new Random();
		
		/*
		 *  nextInt(int N1) + int N2 - 범위 지정
		 *   범위 : N2이상 N1미만
		 *    
		 */
		System.out.println("int형 난수 값 : " + random.nextInt(10) + 1);
	}
}

위와 같이 매개변수(N1)에 (N2) 값을 더해준다면, 

 

범위에서 N2의 값이 시작이 되고, 매개변수는 '11'이 되기 때문에 1 ~ 10까지가 난수 생성 범위가 된다.

 

※예시

- 3 ~ 9까지의 난수 값이 생성되도록 만드시오.

import java.util.Random;

public class Random {

	public static void main(String[] args) {
		
		Random random = new Random();
		
		System.out.println("int형 난수 값 : " + random.nextInt(7) + 3);
	}
}

혹시나 중요하지만 제가 놓친 내용이 있거나, 

 

추가적으로 첨언하시고 싶은 내용이 있다면 댓글로 공유해 주세요.

 

아직은 공부해야 할 내용이 많고 알고 넘어가야 할 것들이 너무나 많습니다.

 

좋은 지식, 좋은 자료를 공유해 주시면 공부하는데 큰 도움이 될 것 같습니다.

 

부족하지만 성실하게 공부해서 저 또한 더욱 유익한 내용을 공유해 드릴 수 있도록 하겠습니다.  

'Java > Class' 카테고리의 다른 글

[Java][Class] String  (0) 2023.04.07
클래스(Class), 객체(Object), 인스턴스(Instance)  (0) 2022.12.20
[Java][Class] BufferedWriter  (0) 2022.12.15
[Java][Class] StringBuilder  (0) 2022.12.09
[Java][Class] StringTokenizer  (0) 2022.12.02
Comments