--- 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).