mercredi 29 juin 2016

Math.random() and .replace() cross-browser

I recently wrote the code to generate 10 characters randomly. Math.random() gives a decimal to toString(36) and all the numbers will be replaced.

Math.random().toString(36).replace(/[^a-z]+/g,'').substr(1,10);

Does anybody have a hint why Firefox (47.0) and Chrome (51) don't handle this equally?

Chrome tests:

Math.random().toString(36).replace(/[^a-z]+/g,'').substr(1,10);
"spkcirhyzb"
"gcqbrmulxe"
"sallvbzqbk"
"pcdcufhqet"
"knfffqsytm"

Firefox tests:

Math.random().toString(36).replace(/[^a-z]+/g,'').substr(1,10);
"zxntpvn"
"hebfyxlt"
"zclj"
"ormtqw"
"cfbsnye"

Live version:

for (var n = 0; n < 5; ++n) {
  console.log(Math.random().toString(36).replace(/[^a-z]+/g,'').substr(1,10));
}



Aucun commentaire:

Enregistrer un commentaire