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.

4

1 回答 1

1

正如 Joel Lee 所说,这是

可播种的 JavaScript 随机数生成器

看来大卫鲍的答案是我需要的:

http://davidbau.com/seedrandom

因为“种子”也可以是一个字符串。

我很抱歉重复。

于 2013-09-07T17:02:31.123 回答