Edrendie
Edrendie

Reputation: 11

Does Math.Random() use the current timestamp to generate random numbers?

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

Answers (1)

Chava Geldzahler
Chava Geldzahler

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:

Upvotes: 4

Related Questions