mercredi 21 mars 2018

How to make a Pseudo Random Number Generator with seed and inputs?

I am trying to make a pseudo random map generator and I am having problems in order to find a proper way to get the "random" numbers.

I need to make function able to accept a set of cordinates (x and y) and return a pseudo random number based on that, and they must always be the exact numbers for the exact input.

Also, I don't need just one random generator, my map need elevation, moisture and temperature so I need several ones. That is why I need be able to insert a seed or third "coordinate" in order to get different numbers for each propiety.

Actually, I found over internet something like this (there were 3 functions that I combine in one), but it not accept 3 inputs:

function PRNG(seed) {
  seed = seed % 2147483647;
  if (seed <= 0) {
    seed += 2147483646;
  }
  random = (seed * 16807) % 2147483647;
  return (random - 1) / 2147483646;
}

I don't understand about bitwise operations and PRNG so I am not sure how can I modify this function.

My idea is do something quite simple, but I don't know if this still is a PRNG or my modifications has broken it:

function PRNG(seed, x, y) {
  seed = seed % 2147483647;
  if (seed <= 0) {
    seed += 2147483646;
  }
  random = (seed * 16807 + x * 1234 + y * 4321) % 2147483647;
  return (random - 1) / 2147483646;
} 

Also, I don't like the idea of seed works in the same way as x and y, because I don't want that the 3 input are aditives (seed + x + y), because that means they could be conmutative, e.g: in coordinates (seed, 3.5, 1) is almost the same as (seed, 0.25, 3.5) (that could be translate in an strange pattern in the map, I guess), it should be different, but I don't know how

From my primitive tests (a loop of 10M iterations and an average), it seems quite random, with an average number of 0.49925 (instead of 0.50).

P.S: I know that this question is a bit strange, if you think this should be migrated to Code Review or be delete I will do that.




Aucun commentaire:

Enregistrer un commentaire