> For the complete documentation index, see [llms.txt](https://nbcamp.gitbook.io/java-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nbcamp.gitbook.io/java-handbook/part-03./undefined.md).

# 임의의 정수 만들기

> ### Math.random() -> 0.0 과 1.0 사이의 임의의 double 값을 반환합니다.
>
> * 0.0 <= Math.random() < 1.0

## 1 부터 5 사이의 random 한 정수 값 구하기

1. 0.0 \* 5 <= Math.random() \* 5 < 1.0 \* 5
2. (int)0.0 <= (int)(Math.random() \* 5) < (int)5.0
3. 0 + 1 <= (int)(Math.random() \* 5) + 1 < 5 + 1
4. 1 <= (int)(Math.random() \* 5) + 1 < 6

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Control4_1 {
    public static void main(String[] args) {
        // 괄호 { } 안의 내용을 20번 반복
        // 1. 1 ~ 10 사이의 난수를 20개 출력하시오.
        // 1,2,3,4,5,6,7,8,9,10

        // 2. -5 ~ 5 사이의 난수를 20개 출력하시오.
        // -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
        for (int i = 0; i < 20; i++) {
            // 1번
//            System.out.println(Math.random() * 10); // 1. 0.0 * 10 <= x * 10 < 1.0 * 10
//            System.out.println((int)(Math.random() * 10)); // 2. 0 <= (int)(x * 10) < 10
//            System.out.println((int)(Math.random() * 10) + 1); // 3. 1 <= (int)(x * 10) + 1 < 11

            // 2번
//            System.out.println((int)(Math.random() * 11)); // 0 ~ 10
            System.out.println((int)(Math.random() * 11) - 5); // -5 ~ 5
        }
    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter4(12)

Ref. [임의의 정수](https://www.youtube.com/watch?v=OPXM_2lagLA\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=35)
{% endhint %}
