본문 바로가기
Programming/JAVA

[JAVA] 랜덤(Random) 수 구현(자바)

by HoonSikE 2022. 2. 19.
반응형
SMALL
Math.random()
package Java;

public class MathRandom {
	public static void main(String[] args) {
		int N = 10;
		// 0.0 <= doubleType1 < 1.0
		double doubleType1 = Math.random();
		// 0.0 <= doubleType2 < N.0
		double doubleType2 = Math.random()*N;
		// 0 <= intType1 < N-1
		int intType1 = (int)Math.random();
		// 1 <= intType2 < N
		int intType2 = (int)(Math.random()*N)+1;
				
		System.out.println(doubleType1);
		System.out.println(doubleType2);
		System.out.println(intType1);
		System.out.println(intType2);
	}
}
 출력
0.34195757462794074
8.491476117299866
0
2

Random rand  = new Random();
package Java;

import java.util.Random;

public class ClassRandom {
	public static void main(String[] args) {
		int N = 10;
		Random rand = new Random();
		// 0 <= intType1 < N
		int intType1 = rand.nextInt(N);
		// 1 <= intType2 <= N
		int intType2 = rand.nextInt(N)+1;
		
		System.out.println(intType1);
		System.out.println(intType2);
	}
}
● 출력
9
7

JAVA List
 

JAVA List

Start Java!!! ● 1 ● 2   기회는 준비된 자에게 찾아온다.

han-hoon.tistory.com


  

기회는 준비된 자에게 찾아온다.

 


 

반응형
LIST

'Programming > JAVA' 카테고리의 다른 글

[JAVA] 반복문 for과 while(자바)  (0) 2022.02.19
[JAVA] 조건문 if와 while (자바)  (0) 2022.02.19
[JAVA] 연산자(자바)  (0) 2022.02.18
[JAVA] 변수(자바)  (0) 2022.02.18
[JAVA] JAVA의 특징(자바)  (0) 2022.02.18

댓글