mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-02-04 17:41:23 +01:00
fix: update colorUtils and probabilityUtils to use seeded randomness
This commit is contained in:
parent
4b341a6590
commit
7d10368ac5
2 changed files with 7 additions and 2 deletions
|
|
@ -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
|
* Convert RGB or RGBA color to HEX
|
||||||
|
|
@ -35,11 +35,14 @@ export const C_12 = [
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of distinct colors
|
* 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
|
* @param {number} count - The count of colors to generate
|
||||||
* @returns {string[]} - The array of HEX color strings
|
* @returns {string[]} - The array of HEX color strings
|
||||||
*/
|
*/
|
||||||
export const getColors = (count: number): string[] => {
|
export const getColors = (count: number): string[] => {
|
||||||
const scaleRainbow = scaleSequential(interpolateRainbow);
|
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(
|
const colors = shuffle(
|
||||||
range(count).map(i => (i < 12 ? C_12[i] : color(scaleRainbow((i - 12) / (count - 12)))?.formatHex()))
|
range(count).map(i => (i < 12 ? C_12[i] : color(scaleRainbow((i - 12) / (count - 12)))?.formatHex()))
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ export const each = (n: number) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Random Gaussian number generator
|
* Random Gaussian number generator
|
||||||
|
* Uses randomNormal.source(Math.random) to ensure it uses the current PRNG
|
||||||
* @param {number} expected - expected value
|
* @param {number} expected - expected value
|
||||||
* @param {number} deviation - standard deviation
|
* @param {number} deviation - standard deviation
|
||||||
* @param {number} min - minimum value
|
* @param {number} min - minimum value
|
||||||
|
|
@ -46,7 +47,8 @@ export const each = (n: number) => {
|
||||||
* @return {number} random number
|
* @return {number} random number
|
||||||
*/
|
*/
|
||||||
export const gauss = (expected = 100, deviation = 30, min = 0, max = 300, round = 0) => {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue