import java.util.Random;
public class RandomEx {
public static void main(String[] args) {
Random random = new Random();
// ๋์ ๋์ง๊ธฐ
boolean randomTF = random.nextBoolean();
System.out.println("๋์ ์๋ค : " + randomTF);
System.out.println("๋์ ๋์ง๊ธฐ");
if( randomTF ) System.out.println("์๋ฉด");
else System.out.println("๋ท๋ฉด");
// ์ ์ํ ๋๋ค ์
// * 4bytes : 32bits (-21์ตxxx ~ 21์ตxxx) ์ฌ์ด์ ๋์๋ฅผ ๋ฐํ
int fourByteRandom = random.nextInt();
System.out.println("fourByteRandom : " + fourByteRandom);
// random.nextInt( N ) : (0~N-1) ์ฌ์ด์ ์์์ด ์ ์๋ฅผ ๋ฐํ
// random.nextInt( 6 ) : (0~5)
// random.nextInt( 6 ) + 1 : (1~6)
// [๊ณต์] random.nextInt( ๊ฐ์ ) + ์์์ซ์
// ์ฃผ์ฌ์ ๋์ง๊ธฐ
int dice = random.nextInt(6) + 1;
System.out.println("์ฃผ์ฌ์ : " + dice);
// ๋ก๋
int lotto = random.nextInt(45) + 1;
System.out.println("๋ก๋๋ฒํธ : " + lotto);
// ์ค์ํ ๋๋ค ์
double randomDouble = random.nextDouble();
System.out.println("์ค์ํ ๋๋ค ์ : " + randomDouble);
}
}
Java
๋ณต์ฌ