0

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
JohnMalkowich
  • 298
  • 3
  • 14
  • So you mean you want a random sequence, but not to include one that is a sequence of increasing values, but other cases (like all decreasing) is OK? (Random usually means it could be *anything*, and might be increasing.) – lurker Sep 07 '13 at 16:52
  • How is it going to be random if you are multiplying by a number that increases in a know manner. You should not be using the i to calculate the "random" number! – epascarello Sep 07 '13 at 16:54
  • @JohnMalkowich have you perused [the Wikipedia page about random number generation?](http://en.wikipedia.org/wiki/Random_number_generation) – Pointy Sep 07 '13 at 16:55

1 Answers1

1

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.

Community
  • 1
  • 1
JohnMalkowich
  • 298
  • 3
  • 14