From e3536c7399e788bb22fa486fd5353ce8ee263174 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 22 Apr 2025 11:50:09 -0700 Subject: [PATCH] Eliminate a call to Math.random() by using sliding 32-bit randomness. --- src/lib/gpu/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/gpu/index.ts b/src/lib/gpu/index.ts index fc7c20a..b9a8b13 100644 --- a/src/lib/gpu/index.ts +++ b/src/lib/gpu/index.ts @@ -274,11 +274,12 @@ export class NanoPowGpu { let start = performance.now() let nonce = 0n let result = 0n + let random = BigInt(Math.floor(Math.random() * 0xffffffff)) + let seed = random do { start = performance.now() - const random0 = Math.floor(Math.random() * 0xffffffff) - const random1 = Math.floor(Math.random() * 0xffffffff) - const seed = (BigInt(random0) << 32n) | BigInt(random1) + random = BigInt(Math.floor(Math.random() * 0xffffffff)) + seed = (seed & 0xffffffffn) << 32n | random if (this.#debug) console.log('seed', seed.toString(16).padStart(16, '0')) await this.#dispatch(this.#searchPipeline, seed, hash, difficulty, effort) const found = !!this.#resultView.getUint32(0) -- 2.34.1