Follow up, if I manage to send two requests at EXACTLY the same time, does Math.Random() generate the same number?
-
3I doubt they use time, look at the source code for the engines. – epascarello Jan 22 '18 at 01:56
-
2I presume you are asking because you want to use it as some kind of security feature. Do not use `Math.random` for security related stuff. – Marco Jan 22 '18 at 01:57
-
Possible duplicate of [What algorithm does Math.random use?](https://stackoverflow.com/questions/10361466/what-algorithm-does-math-random-use) – Marco Jan 22 '18 at 02:14
1 Answers
The spec for Math.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. This function takes no arguments.
Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.
In other words, how the randomness is implemented depends entirely on the JavaScript engine, or browser. It may or may not use the current time as part of its implementation.
Relevant resources:
Here's V8 JavaScript Engine's implementation (as of December 17, 2015)
Related thread on Stack Overflow: How does Math.Random() work in JavaScript?

- 1
- 1

- 3,605
- 1
- 18
- 32