Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Javascript math.random method is seeded

I would like to know when, in a browser environment Javascripts math.random method is seeded.

Is it:

A) When computer is switched on

B) When the browser is launched for the first time on a session

C) Each time the browser is launched

D) When the page with JS code containing math.random is opened for the first time

E) Each time that the page containing math.random is opened

F) When math.random() method is called for the first time

G) Others

like image 248
user2426433 Avatar asked Jul 18 '26 02:07

user2426433


1 Answers

From ECMAScript :

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.

So, it depends on the browser.

As it doesn't make any practical difference (as long as it isn't seeded while your pages run) because the seed is itself kindof random (usually based on the timestamp of seeding time), browser documentations are sparse but here's what Internet Explorer's one says :

The random number generator is seeded automatically when JavaScript is first loaded.

As most other strategies would induce potential errors, I think you can assume there won't be any new seeding between the first call to Math.random and the browser closing.

like image 153
Denys Séguret Avatar answered Jul 19 '26 16:07

Denys Séguret