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()
?
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.
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.
精彩评论