Reputation: 11
Follow up, if I manage to send two requests at EXACTLY the same time, does Math.Random() generate the same number?
Upvotes: 1
Views: 1789
Reputation: 3730
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?
Upvotes: 4