0

I know if you do Math.random() it will return a random number between 0 (inclusive) and 1 (exclusive), but where can I find out how JS chooses to return this number?

The MDN page does not include the lower level code.

According to wikipedia here, there are many methods to get a random number, but which method is JS using.

chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • 2
    See here http://en.wikipedia.org/wiki/Pseudorandom_number_generator - every "random" number is generated from a list of actions which is translated to a list of numbers, then other actions on them and then your "random" number. But there is no effective randomness in those pseudo-generators, since nothing inside a machine is random! – Niccolò Campolungo Jul 19 '13 at 20:50
  • 3
    http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf random ( ) 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*. – Yury Tarabanko Jul 19 '13 at 20:52
  • 1
    possible duplicate of [How does Math.random() work internally in javascript?](http://stackoverflow.com/questions/15071929/how-does-math-random-work-internally-in-javascript) and [How random is JavaScript's Math.random?](http://stackoverflow.com/questions/1062902/how-random-is-javascripts-math-random) – abc123 Jul 19 '13 at 20:52
  • This answer seems to have a pretty good description: http://stackoverflow.com/questions/2344312/how-is-randomness-achieved-with-math-random-in-javascript – Ted Jul 19 '13 at 20:52
  • @LightStyle: That's not really true, you can use real hardware noise to implement actually non-deterministic programs – Niklas B. Jul 19 '13 at 20:53
  • @NiklasB. I didn't really know. Anyway I'm just talking about a PC. Can you provide an example? I'm very intrested in it! – Niccolò Campolungo Jul 19 '13 at 20:54
  • 1
    @LightStyle: See http://en.wikipedia.org/wiki/Hardware_random_number_generator. A readily available implementation is the special [/dev/random](http://en.wikipedia.org/wiki//dev/random) device on Linux systems. – Niklas B. Jul 19 '13 at 20:57

2 Answers2

3

It's up to the JavaScript engine vendor to choose which random number generator they want to use.

As long as it produces a "random" number in the interval [0, 1> they will fit the ECMA specification:

15.8.2.14 random()

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.

Source:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.14

The vendors doesn't list what algorithm they use (as far as I could see) so unless you want to browse through the source code (where possible) you can't find out (easily anyways).

(If you are asking in relation to security: don't use the built-in one from either browsers as they are not suitable for this purpose.)

0

The random number generator math.random() is seeded from the current time, as in Java.

Gravy
  • 12,264
  • 26
  • 124
  • 193
  • 1
    This may be how it's done by most implementations, but it's not required by the standard. – Niklas B. Jul 19 '13 at 21:06
  • @NiklasB. - It is correct to say that it is not required by the standard as there is no mention of how it is done in http://www.ecma-international.org/ecma-262/5.1/ Having said this, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random states that it is seeded from the current time. That is where I based my answer. – Gravy Jul 19 '13 at 21:48
  • 1
    This is only the reference for the Gecko engine's implementation, though – Niklas B. Jul 19 '13 at 21:52