random numbers API

Motivation: Web Applications enter the arena of interactive content
creation/consumption (games, productivity software, etc.). A good PRNG
would be desirable in many situations.

Web Applications that desire to use random numbers have a 4 problems with
the existing Math.random function.

1) The implementation is not "high quality" in that the generated random
numbers tend to be statistically poor compared to other higher quality
PRNGs.

2) The API does not support seeding whereas the same sequence of random
numbers can be generated twice if so desired.

3) It is based on floating points which due to machine differences even in
the presence of seeding could generate different numbers.

4) Alternative implementations in JS suffer even in the presence of
sophisticated JITing VMs from the fact that mathematics is done in doubles
(in the case of addition, subtraction, division and multiplication) and by
converting double -> int -> double (in the case of bitshifts and modulo
division). This makes it both harder to implement a reliable PRNG and it
makes it slow.

I would propose an alternative native code random number API that has the
following characteristic:

- The returned value is based on integers ranging from 0 up to a specified
limit.
- It is initializable with a seed
- It makes a guarantee that no matter on what machine the random number is
generated, that the sequence of random numbers to the same seed is
identical.
- It selects a suitable PRNG algorithm to that end that satisfies a high
standard of statistical qualities of PRNGs and exhibits good runtime
performance.

It could look something like this for example:

var prng = new PRNG(seed);
var x = prng.random(limit);

Received on Friday, 16 November 2012 15:14:17 UTC