Files
2nd/10_Wiki/Topic_Programming/Topic_C/C_Random_Numbers.md
T
Antigravity Agent e9cbf23ab5 docs(10_Wiki): Dev 폴더 누락분 반영 — Topic_Programming으로 통합
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영.

- Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들
  (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거.
- Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/
  Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/
  Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이
  존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존).
- Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/
  JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동.
- 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리.
- Topic_Programming 최종 문서 수: 2784 → 3985.
2026-07-05 00:39:13 +09:00

4.2 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
c-random-numbers C Random Numbers Programming_Language draft conceptual
rand() srand()
seeding random
dice roll example
C 난수
B 0.85 2026-07-04 2026-07-04
c
programming-language
w3schools
random
stdlib
https://www.w3schools.com/c/c_random_numbers.php

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 <stdlib.h>) — 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 seedsrand(time(NULL)) (requiring <time.h>) 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 modulorand() % 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):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-07-04: Initial draft synthesized from the W3Schools "C Random Numbers" page (Astra wiki-curation, P-Reinforce v3.1 format).