Question

I have a question, all though I think its more Math than programming. So sorry if this is the wrong place to post this.

But I want to make a function in Javascript that can do the following:

  • the function has a seed parameter
  • inside the function there is a loop.
  • it has to return a number between 0 and 1 depending on the count and the seed.

so for an example if I have this:

var numbers = giveMeNumbers(3224425, 10);

Should give me 10 "random" numbers between 0 and 1. But it has to be the same numbers it returns EVERYTIME i run this function (with the same seed code ofcause)

function giveMeNumbers(seed, amount) {
     var array = [];
     for (var i=0;i<amount;i++)
     {

         var result = 0; //some cool calculation that always gives the same result

         //example: though a bad example.
         result = seed * i % 6;
         //here I use i to calculate a number but the problem is that i dont want it to be systematic. i mean i only get increasing numbers etc.              

         array.push(result);
     }
     return array;
}

So all in all what I want is a non increasing (or systematic) numbers returned. A perfect result could be the numbers:

0.3345 0.8563 0.2234 0.8594 0.1231 etc.

So that there is no system or increment what so ever in the numbers.

Was it helpful?

Solution

As Joel Lee stated, this is a duplicate of

Seedable JavaScript random number generator.

It seems like the answer by David Bau is what i need:

http://davidbau.com/seedrandom

As the "seed" may as well be a string.

Im sorry for the dublicate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top