开发者

generating "random" numbers that are the same every time with javascript (i.e. seeded random numbers) [duplicate]

开发者 https://www.devze.com 2023-01-23 17:24 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: How to create my own JavaScript Random Number generator that I can also set the seed
This question already has answers here: Closed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消