Custom linear congruential generator in JavaScript
canvas, functional-testing, javascript, lcg, random
Solution
For the generator the formula is (you forgot a modulus in the first part):
current = (multiplier * current * modul + addend) % modulus) / modulus
I realize that you try to optimize it so I updated the fiddle with this so you can use it as a basis for the optimizations:
http://jsfiddle.net/AbdiasSoftware/7VmR9/12/
Problem
I am trying to create a custom linear congruential generator (LCQ) in JavaScript (the one used in glibc). Its properties as it's stated on Wikipedia are: `m=2^31` , `a=1103515245` , `c=12345`. Now I am getting next seed value with ``` x = (1103515245 * x + 12345) % 0x80000000 ; // (The same as &0x7fffffff) ``` Although the generator seems to work, but when the numbers are tested on canvas: ``` cx = (x & 0x3fffffff) % canvasWidth; // Coordinate x (the same for cy) ``` They seem to be horribly biased: http://jsfiddle.net/7VmR9/3/show/ Why does this happen? By choosing a different modulo, the result of a visual test looks much better. The testing JSFiddle is here: http://jsfiddle.net/7VmR9/3/ Update At last I fixed the transformation to canvas coordinates as in this formula: ``` var cx = ((x & 0x3fffffff)/0x3fffffff*canvasWidth)|0 ``` Now the pixel coordinates are not so much malformed as when used the modulo operation. Updated fiddle: http://jsfiddle.net/7VmR9/14/