Skip to content
Advertisement

Custom linear congruential generator in JavaScript

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

JavaScript

Although the generator seems to work, but when the numbers are tested on canvas:

JavaScript

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:

JavaScript

Now the pixel coordinates are not so much malformed as when used the modulo operation.

Updated fiddle: http://jsfiddle.net/7VmR9/14/

Advertisement

Answer

For the generator the formula is (you forgot a modulus in the first part):

JavaScript

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/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement