fix: update colorUtils and probabilityUtils to use seeded randomness

This commit is contained in:
Marc Emmanuel 2026-01-23 09:11:26 +01:00
parent 4b341a6590
commit 7d10368ac5
2 changed files with 7 additions and 2 deletions

View file

@ -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()))
);

View file

@ -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);
}
/**