-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: ๐ฐ[Item 59] ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ตํ๊ณ ์ฌ์ฉํ๋ผ
- Loading branch information
1 parent
07ed25d
commit 7c99ef7
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
title: Item 59 - ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ตํ๊ณ ์ฌ์ฉํ๋ผ | ||
date: 2024-04-20 00:10:00 +0900 | ||
categories: [์ดํํฐ๋ธ ์๋ฐ, chapter9] | ||
tags: [์ดํํฐ๋ธ ์๋ฐ] | ||
--- | ||
|
||
## **๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ฏธ์ํ ๋ ๐ถ๏ธ** | ||
|
||
### **๋ฌด์์ ์ ์ ์์ฑ ์์ ** | ||
|
||
```java | ||
public class RandomTest { | ||
// ํํ์ง๋ง ๋ฌธ์ ๊ฐ ์ฌ๊ฐํ ์ฝ๋! | ||
static Random rnd = new Random(); | ||
static int random(int n){ | ||
return Math.abs(rnd.nextInt()) % n; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int n = 2 * (Integer.MAX_VALUE / 3); | ||
int low = 0; | ||
for (int i = 0; i < 1000000; ++i) | ||
if (random(n) < n/2) | ||
low++; | ||
System.out.println(low); | ||
} | ||
} | ||
``` | ||
์ ์ฝ๋๋ **์ธ ๊ฐ์ง ๋ฌธ์ **๋ฅผ ๋ดํฌํ๊ณ ์๋ค. | ||
|
||
1. n์ด ๊ทธ๋ฆฌ ํฌ์ง ์์ 2์ ์ ๊ณฑ์๋ผ๋ฉด ์ผ๋ง ์ง๋์ง ์์ ๊ฐ์ ์์ด์ด ๋ฐ๋ณต๋๋ค. | ||
2. n์ด 2์ ์ ๊ณฑ์๊ฐ ์๋๋ผ๋ฉด ๋ช๋ช ์ซ์๊ฐ ํ๊ท ์ ์ผ๋ก ๋ ์์ฃผ ๋ฐํ ๋๋ค. | ||
- random ๋ฉ์๋๊ฐ ์ด์์ ์ผ๋ก ๋์ํ๋ค๋ฉด low์ ๊ฐ์ด ๋๋ต 50๋ง์ผ๋ก ๋์์ผ ํ์ง๋ง, 66๋ง์ ๊ฐ๊น์ด ๊ฐ์ ์ป๋๋ค. โ 2/3๊ฐ๋์ด ์ค๊ฐ๊ฐ๋ณด๋ค ๋ฎ์ ์ชฝ์ผ๋ก ์ ๋ ค์๋ค. | ||
3. ์ง์ ํ ๋ฒ์ ๋ฐ๊นฅ์ ์๊ฐ ํ์ด๋์ฌ ์ ์๋ ์กฐ๊ฑด์ด ์๋ค. | ||
- `Math.abs(Integer.MIN_VALUE)`๋ ์ ์ ์ค๋ฒํ๋ก์ฐ ํ์ ๋๋ฌธ์ -2,147,483,648์ด ๋ฐํ๋๋ค. | ||
- `rnd.nextInt()`๊ฐ `Integer.MIN_VALUE`๋ฅผ ๋ฐํํ๋ฉด `Math.abs(rnd.nextInt()) % n`์ ์์๊ฐ ๋ฐํ๋๋ค. | ||
|
||
### **ํด๊ฒฐ ๋ฐฉ๋ฒ ๐จ** | ||
|
||
- ์ด๋ฏธ ์ ๋ง๋ค์ด ๋์ `Random.nextInt(int bound)` ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ง์ ํด๊ฒฐํ ํ์๊ฐ ์๋ค. | ||
- ์๋ฐ 7๋ถํฐ๋ `Random` ๋์ `ThreadLocalRandom`์ ์ฌ์ฉํ์. | ||
- ํฌํฌ-์กฐ์ธ ํ์ด๋ ๋ณ๋ ฌ ์คํธ๋ฆผ์์๋ `SplittableRandom`์ ์ฌ์ฉํ์. | ||
|
||
## **ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ด์ ** | ||
|
||
- ์ ๋ฌธ๊ฐ์ ์ง์๊ณผ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋จธ๋ค์ ๊ฒฝํ์ ํ์ฉํ ์ ์๋ค. | ||
- ํต์ฌ์ ์ธ ์ผ๊ณผ ํฌ๊ฒ ๊ด๋ จ ์๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋๋ผ ์๊ฐ์ ํ๋นํ์ง ์์๋ ๋๋ค | ||
- ๋ฐ๋ก ๋ ธ๋ ฅํ์ง ์์๋ ์ฑ๋ฅ์ด ์ง์ํด์ ๊ฐ์ ๋๋ค | ||
- ํจ์น๋ฅผ ๊ฑฐ๋ญํ ์๋ก ๊ธฐ๋ฅ์ด ์ ์ ๋ง์์ง๋ค. | ||
- ๋ง์ ๊ฐ๋ฐ์๋ค์๊ฒ ๋ฏ์ต์ ์ฝ๋๊ฐ ๋์ด ํ์ ํ๊ธฐ ์ข๋ค. | ||
|
||
## **๋ชฐ๋ผ์ ๋ชป์ฐ์ง๋ ๋ง์** | ||
|
||
- ๋ฉ์ด์ ๋ฆด๋ฆฌ์ฆ๋ง๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ง์ ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋๋ค. | ||
- ์๋ก์ด ๊ธฐ๋ฅ์ ์ฐพ์๋ณด์. | ||
- [Consolidated JDK 8 Release Notes](https://www.oracle.com/java/technologies/javase/8all-relnotes.html#JSERN108), [Consolidated JDK 11 Release Notes](https://www.oracle.com/java/technologies/javase/11all-relnotes.html#JSERN11) | ||
- [spring.io/blog](https://spring.io/blog) | ||
|
||
## **๊ผญ ์์์ผ ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ** | ||
|
||
### **ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ** | ||
|
||
- `java.lang` | ||
- `java.util` | ||
- `java.io` | ||
- `Collection framework` | ||
- `Stream` | ||
- `java.util.concurrent` | ||
|
||
### **๊ณ ํ์ง 3rd Party ๋ผ์ด๋ธ๋ฌ๋ฆฌ** | ||
|
||
- **Guava**: [https://github.com/google/guava](https://github.com/google/guava) | ||
|
||
[The Guava library: What are its most useful and/or hidden features?](https://stackoverflow.com/questions/3759440/the-guava-library-what-are-its-most-useful-and-or-hidden-features) | ||
|
||
- **Apache Commons Lang**: [https://github.com/apache/commons-lang](https://github.com/apache/commons-lang) | ||
|
||
[An Introduction to Apache Commons Lang 3](https://www.baeldung.com/java-commons-lang-3) | ||
|
||
[Apache Commons Lang StringUtils - DZone Java](https://dzone.com/articles/apache-commons-lang) | ||
|
||
[apache-commons-collections-vs-guava](https://www.baeldung.com/apache-commons-collections-vs-guava) | ||
|
||
## **๐ก ํต์ฌ ์ ๋ฆฌ** | ||
- ๋ฐํด๋ฅผ ๋ค์ ๋ฐ๋ช ํ์ง ๋ง์. | ||
- ์์ฃผ ํน๋ณํ ๊ธฐ๋ฅ์ด ์๋๋ผ๋ฉด ๋๊ตฐ๊ฐ ์ด๋ฏธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํํ๋ก ๊ตฌํํด ๋์์ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค. | ||
- ๊ฐ๋ฐํ๊ธฐ๋ณด๋ค๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐพ์๋ณด์. |