mercredi 9 août 2017

Use Timestemp as Seed in Seeded Random Generator but Output is Always the Same

I started to make a dungeon-game that creates randomly new dungeons. There for I have the seededRandom function.

function seededRandom (seed, min = 0, max = 1) {
  const random = ((seed * 9301 + 49297) % 233280) / 233280
  return min + random * (max - min)
}

It will be called inside the createDungeon function with Date.now() as argument for seed

function createDungeon(numberOfRooms, rooms = []) {
  ...
  const randomWidth = Math.floor(Generator.seededRandom(Date.now(), 10, 20))
  const randomHeight = Math.floor(Generator.seededRandom(Date.now(), 10, 20))
  const randomTopLeftCoordinate = Coordinate.create(
    Math.floor(Generator.seededRandom(Date.now(), 10, previousRoom.width)), // x position
    Math.floor(Generator.seededRandom(Date.now(), 10, previousRoom.height)) // y position
  )
  ...
}

Inside my tests I logged the start timestemp, createDungeon return value and end timestemp and apparently Date.now() is not exactly enough.

1502301604075 // start timestemp
[ 
  { width: 19, height: 19, topLeftCoordinate: { x: 18, y: 18 } },
  ...
  { width: 19, height: 19, topLeftCoordinate: { x: 18, y: 18 } },
  { width: 19, height: 19, topLeftCoordinate: { x: 18, y: 18 } } 
] 
1502301604075 // end timestemp

Question

What could I use as seed?




Aucun commentaire:

Enregistrer un commentaire