Frage

Ive been wondering for some time. Is there a good (And fast) way to make an number random while its seeded? is there a good algorithm to convert one number into a seemingly random number.

A little illustration:

specialrand(1) = 8
specialrand(2) = 5
specialrand(3) = 2
specialrand(4) = 5
specialrand(5) = 1
specialrand(1) = 8
specialrand(4) = 5
specialrand(1) = 8

It would be very nice if the output could also be huge numbers.

As a note: I don't want to fill a array and randomize the numbers because I want to be able to feed it huge difference of numbers because I want the same output whenever I restart the program

War es hilfreich?

Lösung

You're not looking for a seeded random number. Instead what I think you're looking for is a hashing function. If you put in the same input and get the same output, that's not random.

If you're looking to generate a sequence of random numbers for a run, but have the same sequence generate from run to run, you can use a random number generator that generates the same sequence given the same seed value.

Thats how most basic pRNG's work. There are more cryptographically secure RNG's out there, but your standard Math.rand() should work to accomplish your needs.

Andere Tipps

Maybe pseudorandom number generators are what you are looking for.

For example the XORshift.

uint32_t xor128(void) {
   static uint32_t x = 123456789;
   static uint32_t y = 362436069;
   static uint32_t z = 521288629;
   static uint32_t w = 88675123;
   uint32_t t;

   t = x ^ (x << 11);
   x = y; y = z; z = w;
   return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}

You could create something like this:

  • take a seed
  • specialrand(5) is a function which takes the fifth random number from this seed
  • or specialrand(5) is a function which gets the first random number from the seed+5

Maybe this is enough for your purpose.

Try setting a key or set of keys then writing a function with an equation to return a new number based on that key:

a very basic example would be:

function specialrand(value) {
key = array (1,2,4,6,8);
  for (k in key) {
      if (k%2 === 0) {
      value -= key[k] * value;
      } else {
      value += key[k] / value;
      }
  }
 return value;
}

however you could create a highly complex equation to generate your 'random' number and ensure you return the same number each time.

You can use Date functionality

Math.valueOfSeed = function(n)
    {
        return Number(new Date(n%9999, n%12, n%30, n%24, n%60, n%60, n%1000));
    };

alert(Math.valueOfSeed(1) + " = " + Math.valueOfSeed(1));
alert(Math.valueOfSeed(2) + " = " + Math.valueOfSeed(2));
alert(Math.valueOfSeed(15) + " = " + Math.valueOfSeed(15));
alert(Math.valueOfSeed(5555) + " = " + Math.valueOfSeed(5555));
alert(Math.valueOfSeed(21212121) + " = " + Math.valueOfSeed(21212121));
alert(Math.valueOfSeed(6554654654) + " = " + Math.valueOfSeed(6554654654));​ 

test is here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top