2

Possible Duplicate:
How to create my own JavaScript Random Number generator that I can also set the seed

So, if I have this function:

function randArr(count, low, high) {
    var result = [];
    for (var i=0; i<count; i++) {
        result.push(seededRand(low, high));
    }
    return result;
}

every time I call randArr(5, 1, 100) I would get the same array back, e.g. [54, 23, 1, 9, 15].

Update: I think this is a dupe, but since commentors seem confused, the question is, how to write seededRand()?

Community
  • 1
  • 1
sprugman
  • 19,351
  • 35
  • 110
  • 163

2 Answers2

1

You want that seededRand function?

You could implement a pseudorandom number function by yourself.

I googled for "javascript mersenne twister" and for example found this page: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/JAVASCRIPT/java-script.html

If the license suits you, you could use that.

AndreKR
  • 32,613
  • 18
  • 106
  • 168
1

You need to implement a random number generator where you could set a seed at the beginning.

I needed to do that a while ago in ActionScript and I used Blum Blum Shub, because its quite easy to implement. Implementing a mersene twister also should be possible, and should give "better random" results.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • 1
    Blum Blum Shub is pretty good, but it does cost a bit more on the processing. Mersenne Twister algorithms are mostly seeded by multiply with carry algorithms. And if you're going to use Mersenne Twister to seed a javascript rand() function, why not use it in the first place? For simple applications, I think multiply-with-carry generators suffice. – LostInTheCode Nov 07 '10 at 16:49