컴퓨터 난이도(쉬움 35%·보통 40%·어려움 25%)와 사람 같은 수, 서버 난수를 OS 보안 난수로
- secureRandom(): crypto.getRandomValues 53비트 실수. 서버의 Math.random(컴퓨터 수·생각 시간·AI 말·방 코드·마감 유예)을 모두 교체 - 컴퓨터 id의 ULID를 secureRandom으로 만들고, 그 무작위 부분 30비트로 난이도·말투(smart)를 정함(botDraw) - humanBest: 오목·체스·장기·오델로·바둑에서 난이도별로 비슷한 수(slip)·사고(lapse)·꼭 둬야 할 수 놓침(miss) - 문서 09 §9.1(확률표·게임별 기준·대국 결과), 06 §12; 테스트 bots/level.test.ts, 레벨별 완주 테스트 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,6 +30,25 @@ export function createSeed(): ChaChaSeed {
|
||||
return [...buf] as ChaChaSeed;
|
||||
}
|
||||
|
||||
/** Pool of OS random words for `secureRandom` (one `getRandomValues` call per 256 numbers). */
|
||||
const pool = new Uint32Array(512);
|
||||
let poolAt = pool.length;
|
||||
|
||||
/**
|
||||
* Uniform float in [0, 1) with 53 random bits from the operating system's secure source (`crypto.getRandomValues`),
|
||||
* a drop-in for `Math.random` wherever the server picks something nobody should be able to predict: computer moves,
|
||||
* thinking times, room codes, computer ids (2026-10-11). Game dice/cards/tiles use the seeded ChaCha20 below.
|
||||
*/
|
||||
export function secureRandom(): number {
|
||||
if (poolAt + 2 > pool.length) {
|
||||
crypto.getRandomValues(pool);
|
||||
poolAt = 0;
|
||||
}
|
||||
const hi = pool[poolAt++]! >>> 5; // 27 bits
|
||||
const lo = pool[poolAt++]! >>> 6; // 26 bits
|
||||
return (hi * 67108864 + lo) / 9007199254740992;
|
||||
}
|
||||
|
||||
export function seedToHex(seed: RngState): string {
|
||||
return seed.map((n) => (n >>> 0).toString(16).padStart(8, '0')).join('');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user