From c590c168f4b41d8e37e09d064288d5d0b77ac28f Mon Sep 17 00:00:00 2001 From: Marc Emmanuel Date: Fri, 23 Jan 2026 13:47:35 +0100 Subject: [PATCH] fix: update colorUtils and probabilityUtils to use seeded randomness (#1280) --- src/utils/colorUtils.ts | 5 ++++- src/utils/probabilityUtils.ts | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils/colorUtils.ts b/src/utils/colorUtils.ts index 047d6eae..e64636fc 100644 --- a/src/utils/colorUtils.ts +++ b/src/utils/colorUtils.ts @@ -1,4 +1,4 @@ -import { color, interpolate, interpolateRainbow, range, RGBColor, scaleSequential, shuffle } from "d3"; +import { color, interpolate, interpolateRainbow, range, RGBColor, scaleSequential, shuffler } from "d3"; /** * Convert RGB or RGBA color to HEX @@ -35,11 +35,14 @@ export const C_12 = [ /** * Get an array of distinct colors + * Uses shuffler with current Math.random to ensure seeded randomness works * @param {number} count - The count of colors to generate * @returns {string[]} - The array of HEX color strings */ export const getColors = (count: number): string[] => { const scaleRainbow = scaleSequential(interpolateRainbow); + // Use shuffler() to create a shuffle function that uses the current Math.random + const shuffle = shuffler(() => Math.random()); const colors = shuffle( range(count).map(i => (i < 12 ? C_12[i] : color(scaleRainbow((i - 12) / (count - 12)))?.formatHex())) ); diff --git a/src/utils/probabilityUtils.ts b/src/utils/probabilityUtils.ts index 4f6fd66a..a526c981 100644 --- a/src/utils/probabilityUtils.ts +++ b/src/utils/probabilityUtils.ts @@ -38,6 +38,7 @@ export const each = (n: number) => { /** * Random Gaussian number generator + * Uses randomNormal.source(Math.random) to ensure it uses the current PRNG * @param {number} expected - expected value * @param {number} deviation - standard deviation * @param {number} min - minimum value @@ -46,7 +47,8 @@ export const each = (n: number) => { * @return {number} random number */ export const gauss = (expected = 100, deviation = 30, min = 0, max = 300, round = 0) => { - return rn(minmax(randomNormal(expected, deviation)(), min, max), round); + // Use .source() to get a version that uses the current Math.random (which may be seeded) + return rn(minmax(randomNormal.source(() => Math.random())(expected, deviation)(), min, max), round); } /**