Files
2nd/10_Wiki/Topics/Frontend/Randomized-Algorithms.md
T
2026-05-10 22:08:15 +09:00

5.7 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-randomized-algorithms Randomized Algorithms 10_Wiki/Topics verified self
Randomized Algorithms
Random Algorithms
확률 알고리즘
none A 0.9 applied
algorithms
randomization
probability
frontend
2026-05-10 pending
language framework
TypeScript 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)

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)

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)

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)

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)

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)

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

🤖 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 패턴