--- id: c-random-numbers title: "C Random Numbers" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["rand() srand()", "seeding random", "dice roll example", "C λ‚œμˆ˜"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "random", "stdlib"] raw_sources: ["https://www.w3schools.com/c/c_random_numbers.php"] applied_in: [] github_commit: "" --- # [[C Random Numbers]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Without calling `srand()`, `rand()` produces the EXACT SAME sequence of numbers every single time the program runs β€” it's not "random" by default at all, just deterministic-looking β€” and the standard fix (seeding with `time(NULL)`) works only because the current time is itself always different, meaning C's "randomness" is really pseudo-randomness bootstrapped from an ever-changing external value. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`rand()`** (from ``) β€” returns a random integer, but deterministic/repeatable unless seeded. [S1] - **`srand(seed)`** β€” sets the starting point for `rand()`'s sequence; without it, the sequence repeats identically on every run. [S1] - **Time as a seed** β€” `srand(time(NULL))` (requiring ``) uses the current time as the seed since it's always changing, producing different sequences run to run. [S1] - **Call `srand()` exactly once** β€” specifically at the start of `main()`, NOT repeatedly inside a loop (re-seeding inside a loop would reset the sequence and undermine randomness). [S1] - **Range restriction via modulo** β€” `rand() % N` maps the result into the range `0` to `N-1`; adding an offset shifts the range further (e.g. `(rand() % 6) + 1` for 1-6). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Unseeded rand() (repeats every run): `int r = rand(); printf("%d\n", r);`. [S1] - Properly seeded rand(): `srand(time(NULL)); printf("%d\n", rand());` β€” different results each run. [S1] - Restricting to a 0-9 range: `int x = rand() % 10; // 0..9`. [S1] - Dice-roll simulation combining modulo and offset: `int dice1 = (rand() % 6) + 1; int dice2 = (rand() % 6) + 1; printf("You rolled %d and %d (total = %d)\n", dice1, dice2, dice1 + dice2);`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μ‹œλ“œ μ—†μ΄λŠ” μ§„μ§œ 랜덀이 μ•„λ‹˜**: srand()λ₯Ό ν˜ΈμΆœν•˜μ§€ μ•ŠμœΌλ©΄ ν”„λ‘œκ·Έλž¨μ„ λͺ‡ λ²ˆμ„ 싀행해도 항상 같은 μˆ«μžκ°€ λ‚˜μ˜¨λ‹€λŠ” 점이 λͺ…μ‹œμ μœΌλ‘œ 경고됨 β€” μ§„μ§œ λ‹€λ₯Έ κ²°κ³Όλ₯Ό μ›ν•˜λ©΄ λ°˜λ“œμ‹œ μ‹œλ“œλ₯Ό μ„€μ •ν•΄μ•Ό 함. [S1] - **srand()λŠ” 반볡문 μ•ˆμ—μ„œ 재호좜 κΈˆμ§€**: main μ‹œμž‘ μ‹œ λ”± ν•œ 번만 ν˜ΈμΆœν•΄μ•Ό ν•˜λ©°, 루프 μ•ˆμ—μ„œ 반볡 ν˜ΈμΆœν•˜λ©΄ μ•ˆ λœλ‹€λŠ” 점이 팁으둜 강쑰됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ£Όμ‚¬μœ„ 두 개λ₯Ό κ΅΄λ € 1-6 λ²”μœ„μ˜ 숫자λ₯Ό 뽑고 합계λ₯Ό κ³„μ‚°ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Simulating a dice roll using rand(), modulo, and an offset (C): ```c #include #include #include int main() { srand(time(NULL)); // seed once, with the current time int dice1 = (rand() % 6) + 1; int dice2 = (rand() % 6) + 1; printf("You rolled %d and %d (total = %d)\n", dice1, dice2, dice1 + dice2); return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Math]], [[C Date Time]], [[C Booleans]] - **μ°Έμ‘° λ§₯락:** μˆ˜ν•™ 및 λ‚œμˆ˜ μ„Ήμ…˜ λ§ˆμ§€λ§‰ β€” λΆˆλ¦¬μ–Έ(Booleans) μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Random Numbers β€” https://www.w3schools.com/c/c_random_numbers.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Random Numbers" page (Astra wiki-curation, P-Reinforce v3.1 format).