From cca8ab61329d2a870a929b5e672f78665dc4ae85 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Wed, 18 Mar 2026 00:07:23 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20No=20compute=20at=20import?= =?UTF-8?q?=20time=20(pre-computed)=20(#959)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of asking node to import the file and immediatelly run computations to have shared globals ready for use, we just precomputed their values ourself. --- src/distribution/uniformFloat32.ts | 5 ++--- src/distribution/uniformFloat64.ts | 8 ++++---- src/generator/congruential32.ts | 2 +- src/generator/mersenne.ts | 5 ++--- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/distribution/uniformFloat32.ts b/src/distribution/uniformFloat32.ts index b73abb0..04762c5 100644 --- a/src/distribution/uniformFloat32.ts +++ b/src/distribution/uniformFloat32.ts @@ -1,8 +1,7 @@ import type { RandomGenerator } from '../types/RandomGenerator'; -const divisor = 1 << 24; -const scale = 1 / divisor; -const mask = divisor - 1; +const scale = 5.960464477539063e-8; // = 1 / divisor (with divisor = 1 << 24) +const mask = 16777215; // = divisor - 1 /** * Uniformly generate random 32-bit floating point values between 0 (included) and 1 (excluded) diff --git a/src/distribution/uniformFloat64.ts b/src/distribution/uniformFloat64.ts index a540eed..91bc325 100644 --- a/src/distribution/uniformFloat64.ts +++ b/src/distribution/uniformFloat64.ts @@ -1,9 +1,9 @@ import type { RandomGenerator } from '../types/RandomGenerator'; -const factor = 1 << 27; -const scale = 2 ** -53; -const mask1 = (1 << 26) - 1; -const mask2 = (1 << 27) - 1; +const factor = 134217728; // = 1 << 27 +const scale = 1.1102230246251565e-16; // = 2 ** -53 +const mask1 = 67108863; // = (1 << 26) - 1 +const mask2 = 134217727; // = (1 << 27) - 1 /** * Uniformly generate random 64-bit floating point values between 0 (included) and 1 (excluded) diff --git a/src/generator/congruential32.ts b/src/generator/congruential32.ts index ee72dd3..81d9001 100644 --- a/src/generator/congruential32.ts +++ b/src/generator/congruential32.ts @@ -6,7 +6,7 @@ import type { JumpableRandomGenerator } from '../types/JumpableRandomGenerator'; const MULTIPLIER: number = 0x000343fd; const INCREMENT: number = 0x00269ec3; const MASK: number = 0xffffffff; -const MASK_2: number = (1 << 31) - 1; +const MASK_2: number = -2147483649; // = (1 << 31) - 1 // Jump constants: equivalent to 2^16 calls to next() (= 3 * 2^16 LCG steps). // Precomputed using the LCG jump-ahead algorithm from: diff --git a/src/generator/mersenne.ts b/src/generator/mersenne.ts index 7b70d44..77f9c3d 100644 --- a/src/generator/mersenne.ts +++ b/src/generator/mersenne.ts @@ -2,7 +2,6 @@ import type { JumpableRandomGenerator } from '../types/JumpableRandomGenerator'; const N = 624; const M = 397; -const R = 31; const A = 0x9908b0df; const F = 1812433253; const U = 11; @@ -11,8 +10,8 @@ const B = 0x9d2c5680; const T = 15; const C = 0xefc60000; const L = 18; -const MASK_LOWER = 2 ** R - 1; -const MASK_UPPER = 2 ** R; +const MASK_LOWER = 2147483647; // = 2 ** R - 1 (with R = 31) +const MASK_UPPER = 2147483648; // = 2 ** R // Derived from https://github.com/numpy/numpy/blob/57fbc869cb10a65da0793ed3aebe00e7f595c0b1/numpy/random/src/mt19937/mt19937-jump.h#L26 const JUMP_COEFS = [ -- 2.51.2