Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : packages/engine/src/rng.ts9 * Module : engine10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Générateur pseudo-aléatoire déterministe (reproductibilité des banques de45 * compétition, des ordres par participant et des simulations statistiques).46 */4748/** Hachage 32 bits (xmur3) d'une chaîne, pour dériver une graine. */49export function hashSeed(text: string): number {50 let h = 1779033703 ^ text.length;51 for (let i = 0; i < text.length; i++) {52 h = Math.imul(h ^ text.charCodeAt(i), 3432918353);53 h = (h << 13) | (h >>> 19);54 }55 h = Math.imul(h ^ (h >>> 16), 2246822507);56 h = Math.imul(h ^ (h >>> 13), 3266489909);57 return (h ^ (h >>> 16)) >>> 0;58}5960export interface Rng {61 /** Réel uniforme dans [0, 1). */62 next(): number;63 /** Entier uniforme dans [min, max] (bornes incluses). */64 int(min: number, max: number): number;65 /** Réel uniforme dans [min, max). */66 range(min: number, max: number): number;67 pick<T>(items: readonly T[]): T;68 shuffle<T>(items: readonly T[]): T[];69 /** Sous-générateur indépendant, dérivé d'une étiquette. */70 fork(label: string): Rng;71}7273export function createRng(seed: string | number): Rng {74 const seedText = String(seed);75 let a = typeof seed === "number" ? seed >>> 0 : hashSeed(seedText);76 const next = (): number => {77 // mulberry3278 a = (a + 0x6d2b79f5) >>> 0;79 let t = a;80 t = Math.imul(t ^ (t >>> 15), t | 1);81 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);82 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;83 };84 const rng: Rng = {85 next,86 int: (min, max) => min + Math.floor(next() * (max - min + 1)),87 range: (min, max) => min + next() * (max - min),88 pick: (items) => {89 if (items.length === 0) throw new Error("pick() sur une liste vide");90 return items[Math.floor(next() * items.length)] as (typeof items)[number];91 },92 shuffle: (items) => {93 const out = items.slice();94 for (let i = out.length - 1; i > 0; i--) {95 const j = Math.floor(next() * (i + 1));96 const tmp = out[i]!;97 out[i] = out[j]!;98 out[j] = tmp;99 }100 return out;101 },102 fork: (label) => createRng(`${seedText}::${label}`),103 };104 return rng;105}106