Skip to content

Commit

Permalink
Merge pull request #115 from toddmedema/seed
Browse files Browse the repository at this point in the history
Store and use seed for randomness
  • Loading branch information
toddmedema authored Apr 30, 2024
2 parents d0e0a8a + 901f6e2 commit 525a90f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export interface ScenarioType {
}

export interface GameType {
seed: number;
difficulty: DifficultyType;
scenarioId: number;
location: LocationType;
Expand Down
27 changes: 25 additions & 2 deletions src/helpers/Math.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getIntersectionX(
line2StartX: number,
line2StartY: number,
line2EndX: number,
line2EndY: number,
line2EndY: number
) {
const denominator =
(line2EndY - line2StartY) * (line1EndX - line1StartX) -
Expand All @@ -22,9 +22,32 @@ export function getIntersectionX(
return line1StartX + (numerator1 / denominator) * (line1EndX - line1StartX);
}

// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
function splitmix32(a: number) {
return function () {
a |= 0;
a = (a + 0x9e3779b9) | 0;
let t = a ^ (a >>> 16);
t = Math.imul(t, 0x21f0aaad);
t = t ^ (t >>> 15);
t = Math.imul(t, 0x735a2d97);
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
};
}

let prng = null as any;

export function seedRandom(seed: number) {
prng = splitmix32(seed);
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
export function getRandomRange(min: number, max: number) {
return Math.random() * (max - min) + min;
if (!prng) {
console.log("prng not seeded, generating now");
seedRandom(Date.now() * Math.random());
}
return prng() * (max - min) + min;
}

// https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
Expand Down
5 changes: 4 additions & 1 deletion src/reducers/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
formatWatts,
formatWattHours,
} from "../helpers/Format";
import { arrayMove } from "../helpers/Math";
import { arrayMove, seedRandom } from "../helpers/Math";
import { getSolarOutputFactor, getWindOutputFactor } from "../helpers/Energy";
import { getFuelPricesPerMBTU } from "../data/FuelPrices";
import { getWeather, getRawSolarIrradianceWM2 } from "../data/Weather";
Expand Down Expand Up @@ -86,6 +86,7 @@ let previousTickMs = 0;
let previousSpeed = "PAUSED" as SpeedType;
let previousMonth = "";
const initialGame: GameType = {
seed: Date.now() * Math.random(),
scenarioId: 0,
location: LOCATIONS["SF"],
difficulty: "Employee",
Expand Down Expand Up @@ -134,6 +135,8 @@ export const gameSlice = createSlice({
initGame: (state, action: PayloadAction<NewGameAction>) => {
const a = action.payload;
state.timeline = [] as TickPresentFutureType[];
state.seed = Date.now() * Math.random();
seedRandom(state.seed);
const scenario =
SCENARIOS.find((s) => s.id === state.scenarioId) || SCENARIOS[0];
state.date = getDateFromMinute(0, scenario.startingYear);
Expand Down

0 comments on commit 525a90f

Please sign in to comment.