Skip to content

Commit c590c16

Browse files
fix: update colorUtils and probabilityUtils to use seeded randomness (#1280)
1 parent 70ed9ae commit c590c16

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/utils/colorUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { color, interpolate, interpolateRainbow, range, RGBColor, scaleSequential, shuffle } from "d3";
1+
import { color, interpolate, interpolateRainbow, range, RGBColor, scaleSequential, shuffler } from "d3";
22

33
/**
44
* Convert RGB or RGBA color to HEX
@@ -35,11 +35,14 @@ export const C_12 = [
3535

3636
/**
3737
* Get an array of distinct colors
38+
* Uses shuffler with current Math.random to ensure seeded randomness works
3839
* @param {number} count - The count of colors to generate
3940
* @returns {string[]} - The array of HEX color strings
4041
*/
4142
export const getColors = (count: number): string[] => {
4243
const scaleRainbow = scaleSequential(interpolateRainbow);
44+
// Use shuffler() to create a shuffle function that uses the current Math.random
45+
const shuffle = shuffler(() => Math.random());
4346
const colors = shuffle(
4447
range(count).map(i => (i < 12 ? C_12[i] : color(scaleRainbow((i - 12) / (count - 12)))?.formatHex()))
4548
);

src/utils/probabilityUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const each = (n: number) => {
3838

3939
/**
4040
* Random Gaussian number generator
41+
* Uses randomNormal.source(Math.random) to ensure it uses the current PRNG
4142
* @param {number} expected - expected value
4243
* @param {number} deviation - standard deviation
4344
* @param {number} min - minimum value
@@ -46,7 +47,8 @@ export const each = (n: number) => {
4647
* @return {number} random number
4748
*/
4849
export const gauss = (expected = 100, deviation = 30, min = 0, max = 300, round = 0) => {
49-
return rn(minmax(randomNormal(expected, deviation)(), min, max), round);
50+
// Use .source() to get a version that uses the current Math.random (which may be seeded)
51+
return rn(minmax(randomNormal.source(() => Math.random())(expected, deviation)(), min, max), round);
5052
}
5153

5254
/**

0 commit comments

Comments
 (0)