๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

Java Random , Math ๋ณธ๋ฌธ

Language/Java

Java Random , Math

๊ธฐ๋Œ 2023. 11. 16. 14:49
728x90

๐Ÿ“š ๊ตญ๋น„ 23 ์ผ์ฐจ


๐Ÿ”ธ Random

  • ๋‚œ์ˆ˜ ๋ฐœ์ƒ ๊ด€๋ จ ํ•จ์ˆ˜
  • ๋žœ๋คํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑํ›„ ์‚ฌ์šฉ๊ฐ€๋Šฅ
import java.util.Random;

public class RandomApp {
    public static void main(String[] args) {

        Random random = new Random();
        
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        	}
        }

 

nextInt(): ์ •์ˆ˜ ๋ฒ”์œ„ ๋‚ด์—์„œ ๋ฌด์ž‘์œ„ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
int randomNum = random.nextInt();

nextInt(int bound): 0๋ถ€ํ„ฐ bound ์‚ฌ์ด์˜ ๋ฌด์ž‘์œ„ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
int randomNumI = random.nextInt(100); // 0๋ถ€ํ„ฐ 99๊นŒ์ง€์˜ ๋‚œ์ˆ˜

nextLong(): long ๋ฒ”์œ„ ๋‚ด์—์„œ ๋ฌด์ž‘์œ„ long ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
long randomLong = random.nextLong();

nextDouble(): 0.0๋ถ€ํ„ฐ 1.0 ๋ฏธ๋งŒ์˜ ๋ฌด์ž‘์œ„ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
double randomDl = random.nextDouble();

// nextFloat(): 0.0๋ถ€ํ„ฐ 1.0 ๋ฏธ๋งŒ์˜ ๋ฌด์ž‘์œ„ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
float randomFl = random.nextFloat();

// nextBoolean(): true ๋˜๋Š” false ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
boolean randomBl = random.nextBoolean();

 

// 1 ~ 45 ์ž„์˜์˜ ์ •์ˆ˜๊ฐ’ ๋ฐ›๊ธฐ

        for (int i = 1; i <= 6; i++) {
            int num = random.nextInt(45) + 1;
            System.out.println(num);
        }
   // ๋กœ๋˜ ๋ฒˆํ˜ธ 6๊ฐœ ๋ฐ›๊ธฐ
        // ์ค‘๋ณต์ด ์—†์–ด์•ผํ•˜๋ฉฐ , ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
        int[] lotto = new int[6];
        int position = 0;  // ๋ฒˆํ˜ธ ์œ„์น˜ ๋‹ด๋‹น
        while (true) {

            int num = random.nextInt(45) + 1;

            // ์ค‘๋ณต๊ฒ€์‚ฌ , ๋ฐฐ์—ด์— ๋ฒˆํ˜ธ ์ €์žฅํ•˜๊ธฐ
            if (position == 0) {
                lotto[position++] = num;
            } else {
                // ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜ ํ”Œ๋ž˜๊ทธ ๋ณ€์ˆ˜ ๋ผ๊ณ ํ•œ๋‹ค, ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ false
                boolean isExist = false;
                for (int index = 0; index <= position; index++) {

                    if (lotto[index] == num) {
                        isExist = true;
                        break;
                    } // if์ข…๋ฃŒ

                } // for ์ข…๋ฃŒ
                // isExist๊ฐ€ false์ด๋ฉด ๋ฐฐ์—ด์— ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅ
                if (!isExist) {
                    lotto[position++] = num;
                }
            }
            if (position == 6) {
                break;
            }

        } // while ์ข…๋ฃŒ
        Arrays.sort(lotto);
        System.out.println("๋กœ๋˜ ๋ฒˆํ˜ธ : " + Arrays.toString(lotto));

๐Ÿ”ธ Math

  • ์ˆ˜ํ•™ ์—ฐ์‚ฐ๊ณผ ๊ด€๋ จ๋œ static ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค 
  • Math ํด๋ž˜์Šค๋Š” ์ƒ์ˆ˜์™€ static ๋ฉ”์„œ๋“œ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํด๋ž˜์Šค
// Math ํด๋ž˜์Šค์˜ ์ƒ์ˆ˜๊ฐ’
System.out.println(Math.PI);
System.out.println(Math.E);

//        int abs(int x) , long abs(long x) , double abs(long x)
//          - ์ ˆ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜

System.out.println("์ ˆ๋Œ€ ๊ฐ’ : " + Math.abs(10));	// 10
System.out.println("์ ˆ๋Œ€ ๊ฐ’ : " + Math.abs(-10));	// 10

// int min(int x, int y) , long min(long x , long y) , double min(double x , double y)
// - ๋‘ ์ˆ˜์ค‘์—์„œ ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

// int max( int x, int y) , long max(long x , long y)
// - ๋‘ ์ˆ˜ ์ค‘์—์„œ ํฐ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

System.out.println("์ž‘์€์ˆ˜ ๋ฐ˜ํ™˜ : " + Math.min(100, 50));	// 50
System.out.println("ํฐ ์ˆ˜ ๋ฐ˜ํ™˜ : " + Math.max(200, 100));	// 200

// double pow(double a, double b )
// - a^b๊ฐ’์„ ๋ฐ˜ํ™˜           ^ ๋Š” ์ œ๊ณฑ์„ ์˜๋ฏธํ•œ๋‹ค  2์˜ 3์Šน -> 2 * 2 * 2 = 8
System.out.println(Math.pow(2, 3)); // 8


// int round(float x) , long round(double x)
// - ๋ฐ˜์˜ฌ๋ฆผ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜

// ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ์งธ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ •์ˆ˜๊ฐ€ ์™„์„ฑ๋œ๋‹ค
System.out.println(Math.round(3.14));   // 3
System.out.println(Math.round(3.45));   // 3
System.out.println(Math.round(3.56));   // 4

// ๋ฒ„๋ฆผ double floor(double x)
System.out.println("floor ๋ฒ„๋ฆผ : " + Math.floor(3.45));
System.out.println("floor ๋ฒ„๋ฆผ : " + Math.floor(4.89));

// ์˜ฌ๋ฆผ double ceil(double x)
System.out.println("ceil ์˜ฌ๋ฆผ : " + Math.ceil(4.17));
System.out.println("ceil ์˜ฌ๋ฆผ : " + Math.ceil(3.59));

 

728x90

'Language > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Java ์ œ๋„ค๋ฆญ, ์ปฌ๋ ‰์…˜ , Set , List , Map , Stack  (1) 2023.11.20
Java Iterator , Score CRUD  (1) 2023.11.17
Java Wrapper , Date , Calendar , StringBuilder , Buffer  (6) 2023.11.15
Java System , String  (1) 2023.11.14
Java final , Object , toString  (1) 2023.11.13