2

I need a function that will generate a number between two other numbers, based on a given number.

If the function looks like this:

function getNumber(minimum, maximum, number) {
  ...
}

And if I input this:

getNumber(0, 3, 12837623);

It should process the third argument, and give me a number between 0 and 3 (inclusive or non-inclusive), based on it, and if I run it multiple times with the same arguments I should get the same number.

It is kind of similar to a random number function, except it uses the given number instead of a random one.

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}

function getNumber(min, max, number) {
  //uses third argument instead of random number
  return Math.floor(number * (max - min + 1) ) + min;
}

1 Answers1

0

Just use a Seeded PsuedoRandom number generator (lots of them here).

A pseudorandom number generator would always give the same series with a particular seed. Just use the third argument as your seed.

Infact Math.random() is also a pseduorandom number generator, the only problem is its seed cannot be chosen. This is the reason Math.random() should NEVER be used in cryptography related implementations. (there is window.crypto.getRandomValues() for it.)

So your new random number function should be

function getRndInteger(min, max, seed) {
  return Math.floor(SeedRandom(seed) * (max - min + 1) ) + min;
}
Anunay
  • 397
  • 2
  • 10