toddmedema / electrify

Take Charge of the Power Market
http://electrifygame.com
MIT License
6 stars 2 forks source link

improve randomness: store + use seed #78

Closed toddmedema closed 2 months ago

toddmedema commented 3 months ago
  1. When you start a game, store a seed
  2. Use that seed for all randomness calls
  3. Then, could have seed as an input for https://github.com/toddmedema/electrify/issues/76
toddmedema commented 2 months ago

Based on https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript it sounds like I can just use this function:

function splitmix32(a) {
 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;
  }
}

const prng = splitmix32(Date.now() * Math.random())
for(let i=0; i<10; i++) console.log(prng());