Files
2nd/10_Wiki/Topics/Frontend/Randomized-Algorithms.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

178 lines
5.6 KiB
Markdown

---
id: wiki-2026-0508-randomized-algorithms
title: Randomized Algorithms
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Randomized Algorithms, Random Algorithms, 확률 알고리즘]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [algorithms, randomization, probability, frontend]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: TypeScript
framework: none
---
# Randomized Algorithms
## 매 한 줄
> **"매 randomness 의 algorithmic resource 사용"**. Randomized algorithm 은 worst-case input adversary 회피 또는 simpler logic 으로 expected-time 성능 달성. Frontend 에서 quickselect, reservoir sampling, bloom filter, A/B bucketing 등 매 광범위 응용.
## 매 핵심
### 매 두 가지 분류
- **Las Vegas**: 매 항상 correct 결과, runtime 만 random (e.g. randomized quicksort).
- **Monte Carlo**: 매 bounded runtime, 결과는 매 probability 1-ε 로 correct (e.g. Miller-Rabin, bloom filter).
### 매 왜 frontend
- 매 large dataset (virtualized list, large table) 의 sampling.
- 매 client-side A/B test bucketing — deterministic hash + random salt.
- 매 game / animation 의 procedural generation.
- 매 dedup / membership test (bloom filter) 의 memory 절약.
### 매 응용
1. Reservoir sampling — log streaming 의 fixed-size sample.
2. Quickselect — top-K 의 O(n) average.
3. Bloom filter — 매 service worker cache lookup.
4. Random shuffle — 매 Fisher-Yates 만 정답.
## 💻 패턴
### Fisher-Yates shuffle (매 in-place, uniform)
```ts
function shuffle<T>(arr: T[]): T[] {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
```
### Reservoir sampling (k items from stream)
```ts
function reservoir<T>(stream: Iterable<T>, k: number): T[] {
const reservoir: T[] = [];
let i = 0;
for (const item of stream) {
if (i < k) reservoir.push(item);
else {
const j = Math.floor(Math.random() * (i + 1));
if (j < k) reservoir[j] = item;
}
i++;
}
return reservoir;
}
```
### Quickselect (kth smallest, O(n) avg)
```ts
function quickselect(arr: number[], k: number, lo = 0, hi = arr.length - 1): number {
if (lo === hi) return arr[lo];
const pivotIdx = lo + Math.floor(Math.random() * (hi - lo + 1));
const pivot = arr[pivotIdx];
[arr[pivotIdx], arr[hi]] = [arr[hi], arr[pivotIdx]];
let store = lo;
for (let i = lo; i < hi; i++) {
if (arr[i] < pivot) {
[arr[i], arr[store]] = [arr[store], arr[i]];
store++;
}
}
[arr[store], arr[hi]] = [arr[hi], arr[store]];
if (k === store) return arr[store];
return k < store ? quickselect(arr, k, lo, store - 1) : quickselect(arr, k, store + 1, hi);
}
```
### Bloom filter (membership, frontend cache)
```ts
class BloomFilter {
private bits: Uint8Array;
constructor(private size: number, private k: number) {
this.bits = new Uint8Array(Math.ceil(size / 8));
}
private hash(s: string, seed: number): number {
let h = seed;
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0;
return h % this.size;
}
add(s: string) {
for (let i = 0; i < this.k; i++) {
const idx = this.hash(s, i * 0x9e3779b1);
this.bits[idx >> 3] |= 1 << (idx & 7);
}
}
has(s: string): boolean {
for (let i = 0; i < this.k; i++) {
const idx = this.hash(s, i * 0x9e3779b1);
if (!(this.bits[idx >> 3] & (1 << (idx & 7)))) return false;
}
return true; // possible false positive
}
}
```
### A/B bucketing (매 deterministic hash + salt)
```ts
import { sha256 } from "@noble/hashes/sha256";
function bucket(userId: string, exp: string, buckets: number): number {
const h = sha256(`${exp}:${userId}`);
const n = (h[0] << 24) | (h[1] << 16) | (h[2] << 8) | h[3];
return (n >>> 0) % buckets;
}
```
### Crypto-secure random (매 token, ID)
```ts
function secureId(bytes = 16): string {
const a = new Uint8Array(bytes);
crypto.getRandomValues(a);
return Array.from(a, b => b.toString(16).padStart(2, "0")).join("");
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Top-K from large array | Quickselect (O(n) avg) |
| Sample N from stream | Reservoir sampling |
| Membership test, big set | Bloom filter |
| Token / session ID | `crypto.getRandomValues` |
| A/B bucket | SHA-256 hash + modulo |
| Shuffle | Fisher-Yates 만 |
**기본값**: deterministic algorithm 우선 — randomization 은 매 measured benefit 있을 때 만.
## 🔗 Graph
- 부모: [[Probability]]
- 변형: [[Monte-Carlo-Methods]]
- 응용: [[Bloom-Filter]] · [[Reservoir-Sampling]]
## 🤖 LLM 활용
**언제**: large dataset 의 sampling/selection, A/B bucketing, client-side dedup, security-irrelevant random.
**언제 X**: cryptographic context 에 `Math.random` — 매 절대 X. Token, password, nonce 매 `crypto.getRandomValues` 만.
## ❌ 안티패턴
- **Math.random for security**: 매 PRNG 의 predictability — token 에 절대 X.
- **Naive shuffle (`sort(() => Math.random() - 0.5)`)**: 매 non-uniform distribution.
- **Modulo bias**: `Math.floor(Math.random() * n)` 까지는 OK, 매 `crypto` byte % n 매 bias 발생 가능.
- **Repeated bloom filter without size planning**: 매 false positive rate 폭증.
## 🧪 검증 / 중복
- Verified (CLRS Ch.5, Mitzenmacher & Upfal "Probability and Computing", MDN Web Crypto).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Las Vegas/Monte Carlo 분류, Fisher-Yates/Reservoir/Quickselect/Bloom/AB-bucket 패턴 |