The JavaScript language has a built-in Math.random() function that emits random numbers between 0.0 and 1.0 but that RNG is not seedable so you get different values each time the function is called even in situations where you want reproducible results.

Some time ago I coded up a seedable JavaScript RNG that is very crude. It worked by taking the trigonometric sin of the previous generated value and then using just the decimal part.

For fun I decided to see if I could implement the Wichmann-Hill algorithm in JavaScript. Wichmann-Hill is an old algorithm but is pretty effective. It scrambles up three sort of random values and then combines them to get a pretty good random value.

My Wichmann-Hill RNG looks good enough for casual use but I didn’t subject it to thorough testing.

Note: Thanks to Stephen Jones (Consilio) who pointed out weaknesses of the sin-algorithm and suggested taking a look at the XOR-shift algorithm.

### Like this:

Like Loading...

*Related*