Search

Random

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
๋ณต์‚ฌ