ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA 37] 자바 랜덤 / 무작위 ( Random )
    Language/Java 2018. 2. 26. 15:45
    반응형


    ■ Random ( 랜덤 )


    1. With Random

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.util.*;
     
    class RandomS2{
        Random random = new Random();
     
        void m1(){        // 방법 1. With Random 
            int a = 3;
            int b = random.nextInt(a);    // 0 , 1 , 2 >> 0 ~ ( n-1 )
            System.out.println("b : " + b);
     
            boolean c = random.nextBoolean();
            System.out.println("c : " + c);
        }
     
        public static void main(String[] args){
            RandomS2 rs2 = new RandomS2();
            rs2.m1();
        }
    }
    cs



    2. With Math

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import java.util.*;
     
    class RandomS2{
        Random random = new Random();
     
        void m1(){        // 방법 2. With Math 
            int a = 3;
            int b = Math.random();
            System.out.println("b : " + b);
        }
     
        public static void main(String[] args){
            RandomS2 rs2 = new RandomS2();
            rs2.m1();
        }
    }
    cs


    error : incompatioble type : possible lossy conversion from fouble to int

    에러 : 호환되지 않는 타입 : double 에서 int 로 변형시에 정보손실 가능성이 있다.

    int b = Math.random();


     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import java.util.*;
     
    class RandomS2{
        Random random = new Random();
     
        void m1(){        // 방법 2. With Math 
            int a = 3;
            double b = Math.random();    // 0 < 값 < 1
            int c = (int)(a*b);        // 0 , 1 , 2
            System.out.println("c : " + c);
        }
     
        public static void main(String[] args){
            RandomS2 rs2 = new RandomS2();
            rs2.m1();
        }
    }
    cs





    3. 기본

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.*;
     
    class RandomS1{        // Random Study 1
        Random random = new Random();
     
        void showRandom(){
            int i = random.nextInt(7);    // 0 ~ (n-1) >> 0 ~ 6 >> 7개
            System.out.println("i : " + i);
        }
     
        public static void main(String[] args){
            new RandomS1().showRandom();
        }
    }
    cs


    0 ~ 6 까지 random.nextInt(7);

    0 , 1 , 2 , 3 , 4 , 5 , 6 

    즉 7개 중에 하나



    2. 응용

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import java.util.*;
     
    class RandomS1{        // Random Study 1
        Random random = new Random();
     
        void showRandom(){
            int i = random.nextInt(7);    // 0 ~ (n-1) >> 0 ~ 6 >> 7개
            System.out.println("i : " + i);
            switch(i){
                case 0 : System.out.print("일"); break;
                case 1 : System.out.print("월"); break;
                case 2 : System.out.print("화"); break;
                case 3 : System.out.print("수"); break;
                case 4 : System.out.print("목"); break;
                case 5 : System.out.print("금"); break;
                case 6 : System.out.print("토");
            }
            System.out.println("요일");
        }
     
        public static void main(String[] args){
            new RandomS1().showRandom();
        }
    }
    cs



    반응형
Designed by Tistory.