9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
142 lines
5.6 KiB
Markdown
142 lines
5.6 KiB
Markdown
---
|
||
id: javascript-random
|
||
title: "JavaScript Random"
|
||
category: "Frontend"
|
||
status: "draft"
|
||
verification_status: "conceptual"
|
||
canonical_id: ""
|
||
aliases: ["Math.random", "random number", "random integer", "getRndInteger", "JS random"]
|
||
duplicate_of: ""
|
||
source_trust_level: "B"
|
||
confidence_score: 0.9
|
||
created_at: 2026-06-23
|
||
updated_at: 2026-06-23
|
||
review_reason: ""
|
||
merge_history: []
|
||
tags: ["javascript", "js", "web", "frontend", "w3schools", "math", "random"]
|
||
raw_sources: ["https://www.w3schools.com/js/js_random.asp"]
|
||
applied_in: []
|
||
github_commit: ""
|
||
---
|
||
|
||
# [[JavaScript Random]]
|
||
|
||
## 🎯 한 줄 통찰 (One-line insight)
|
||
`Math.random()` returns a float in [0, 1); wrapping it in `Math.floor(Math.random() * n)` yields a random integer, and the inclusive-range formula `Math.floor(Math.random() * (max - min + 1)) + min` covers both endpoints. [S1]
|
||
|
||
## 🧠 핵심 개념 (Core concepts)
|
||
- **`Math.random()`** always returns a number lower than 1 (and ≥ 0). [S1]
|
||
- **Random integers** are produced by combining `Math.random()` with `Math.floor()`. [S1]
|
||
- **Multiplying by `n`** then flooring gives an integer from 0 to n−1; using `n+1` makes the top bound inclusive. [S1]
|
||
- **Adding an offset** (`+ 1`, `+ min`) shifts the range away from 0. [S1]
|
||
- **The inclusive-range function** uses `(max - min + 1)` so that both `min` and `max` are possible results. [S1]
|
||
|
||
## 🧩 추출된 패턴 (Extracted patterns)
|
||
- **0 to n−1** — `Math.floor(Math.random() * n)`. [S1]
|
||
- **0 to n (inclusive)** — `Math.floor(Math.random() * (n + 1))`. [S1]
|
||
- **1 to n (inclusive)** — `Math.floor(Math.random() * n) + 1`. [S1]
|
||
- **min to max, max excluded** — `Math.floor(Math.random() * (max - min)) + min`. [S1]
|
||
- **min to max, max included** — `Math.floor(Math.random() * (max - min + 1)) + min`. [S1]
|
||
|
||
## 📖 세부 내용 (Details)
|
||
**Math.random()** — `Math.random()` returns a random number between 0 (inclusive), and 1 (exclusive). `Math.random()` always returns a number lower than 1: [S1]
|
||
```javascript
|
||
// Returns a random number:
|
||
Math.random();
|
||
```
|
||
|
||
**JavaScript Random Integers** — `Math.random()` used with `Math.floor()` can be used to return random integers. [S1]
|
||
|
||
Return a random integer from 0 to 9 (both included): [S1]
|
||
```javascript
|
||
// Return a random integer from 0 to 9 (both included):
|
||
Math.floor(Math.random() * 10);
|
||
```
|
||
|
||
Return a random integer from 0 to 10 (both included): [S1]
|
||
```javascript
|
||
// Return a random integer from 0 to 10 (both included):
|
||
Math.floor(Math.random() * 11);
|
||
```
|
||
|
||
Return a random integer from 0 to 99 (both included): [S1]
|
||
```javascript
|
||
// Return a random integer from 0 to 99 (both included):
|
||
Math.floor(Math.random() * 100);
|
||
```
|
||
|
||
Return a random integer from 0 to 100 (both included): [S1]
|
||
```javascript
|
||
// Return a random integer from 0 to 100 (both included):
|
||
Math.floor(Math.random() * 101);
|
||
```
|
||
|
||
Return a random integer from 1 to 10 (both included): [S1]
|
||
```javascript
|
||
// Return a random integer between 1 and 10 (both included):
|
||
Math.floor(Math.random() * 10) + 1;
|
||
```
|
||
|
||
Return a random integer from 1 to 100 (both included): [S1]
|
||
```javascript
|
||
// Returns a random integer from 1 to 100 (both included):
|
||
Math.floor(Math.random() * 100) + 1;
|
||
```
|
||
|
||
**A Proper Random Function** — As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This function returns a random number between `min` (included) and `max` (excluded): [S1]
|
||
```javascript
|
||
function getRndInteger(min, max) {
|
||
return Math.floor(Math.random() * (max - min) ) + min;
|
||
}
|
||
```
|
||
|
||
This function returns a random number between `min` and `max` (both included): [S1]
|
||
```javascript
|
||
function getRndInteger(min, max) {
|
||
return Math.floor(Math.random() * (max - min + 1) ) + min;
|
||
}
|
||
```
|
||
|
||
## 🛠️ 적용 사례 (Applied in summary)
|
||
The page's own snippets are the canonical applied examples — generating random integers in fixed ranges (0–9, 0–100, 1–100) and the reusable `getRndInteger(min, max)` helpers in both exclusive-max and inclusive-max forms. No external project/commit applications found in the source.
|
||
|
||
## 💻 코드 패턴 (Code patterns)
|
||
Random integer 0 to n−1:
|
||
```javascript
|
||
Math.floor(Math.random() * 10); // 0..9
|
||
```
|
||
Random integer 1 to n (inclusive):
|
||
```javascript
|
||
Math.floor(Math.random() * 100) + 1; // 1..100
|
||
```
|
||
Reusable inclusive-range function:
|
||
```javascript
|
||
function getRndInteger(min, max) {
|
||
return Math.floor(Math.random() * (max - min + 1) ) + min;
|
||
}
|
||
```
|
||
|
||
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
||
- **Exclusive-max vs inclusive-max** — `getRndInteger` with `(max - min)` excludes `max`; the variant with `(max - min + 1)` includes `max`. Choose based on whether the upper bound should be a possible result. [S1]
|
||
|
||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||
No contradictions found in the source.
|
||
|
||
## ✅ 검증 상태 및 신뢰도
|
||
- **상태:** draft
|
||
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||
- **신뢰 점수:** 0.90
|
||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||
|
||
## 🔗 지식 그래프 (Knowledge Graph)
|
||
- **상위/루트:** [[JavaScript Tutorial]]
|
||
- **관련 개념:** [[JavaScript Math]], [[JavaScript Numbers]], [[JavaScript Array Sort]]
|
||
- **참조 맥락:** Referenced whenever randomized values or random integer ranges are needed.
|
||
|
||
## 📚 출처 (Sources)
|
||
- [S1] W3Schools — JavaScript Random — https://www.w3schools.com/js/js_random.asp
|
||
|
||
## 📝 변경 이력 (Change history)
|
||
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Random" page (Astra wiki-curation, P-Reinforce v3.1 format).
|