JAVA

3일차// Random 난수 뽑기

aesup 2021. 1. 13. 11:22
728x90

Random 난수 뽑기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
public class MainClass {
    public static void main(String[] args) {
    
        /*
        
        Random : 무작위, 난수
                1 ~ 5 -> ?
        
        
        */
        
        int r; //Random 값
        
        
        //0~9까지의 숫자를 뽑아보자
        r = (int)(Math.random() * 10); 
        System.out.println("r = "+ r);
        
        
        // 10 20 30 40 50
        r = ((int)(Math.random() * 5+ 1 )* 10;  
        
        //개수가 5 개라 5 0~4이기 때문에 여기서 1을 더하고 10을 곱해준다 
        
        System.out.println("r = "+ r);
        //int 를 주는 이유는 소수점을 때기 위해
        
}
}
 
cs

 

 

728x90