Forum Moderators: open

Message Too Old, No Replies

about mathRandom()

         

ctoz

10:41 pm on Jan 26, 2010 (gmt 0)

10+ Year Member



I had a search around,but couldn't find answers to these questions:
what algorithm does mathRandom() use?
to how many decimal places is the calculation done?

claus

11:31 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AFAIK the JavaScript Math.random() originally used the same algorithm as the Java Math.random() which, again, is the nextDouble method of the Random class. It goes like this:


public double nextDouble() {
return (((long)next(26) << 27) + next(27))
/ (double)(1L << 53);
}

Source: [java.sun.com...]

JavaScript's standard library follows Java's naming conventions, and JavaScript's Math and Date objects are based on classes from Java 1.0.
Source: [en.wikipedia.org...]

However, in the latest official standard for JavaScript, called ECMA-script, this is written:

Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy
ECMA-262, December 2009: [ecma-international.org...]

So: Most likely it depends on the browser, and most likely the browser maker won't tell you.

As for decimal places, that depends on your PC hardware, your operating system, and most likely also on your browser. So that would be unpredictable, but certainly not random ;)

ctoz

12:58 am on Jan 27, 2010 (gmt 0)

10+ Year Member



hey, thanks