Seed random number
I've been thinking for a while now. Is there a good (and quick) way to randomize the number while seeding it? there is a good algorithm for converting one number to an apparent random number.
Small 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 a huge number.
As a side note: I don't want to populate the array and randomize the numbers because I want to be able to feed it a huge difference in numbers because I want the same output when I restart the program
You are not looking for a random number. Instead, I think you are looking for a hashing function. If you insert the same input and get the same result, it is no coincidence.
If you want to generate a sequence of random numbers to run, but have the same generation sequence from run to run, you can use a random number generator that generates the same sequence as the same seed.
This is how the main pRNG works. There is a more cryptographically secure RNG, but your standard Math.rand () should work to suit your needs.
Perhaps pseudo random number generators are what you are looking for.
For example 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 can create something like this:
- take the seed
- specialrand (5) is a function that takes the fifth random number from this seed
- or specialrand (5) is a function that gets the first random number from seed + 5
Perhaps this is sufficient for your purpose.
Try setting a key, or a set of keys, and then writing a function with an equation to return a new number based on that key:
a very simple example:
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 very complex equation to generate your "random" number and ensure that you return the same number every time.
Date function can be used
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 here