I have been using Math.floor(Math.random() * someArray.length) for years to choose a random element in an array. I have always wondered why it is better than Date.now() % someArray.length. The latter seems to me much less error prone and must be faster (though, I haven't actually benchmarked it).
Notes:
- I don't need repeatability.
- This isn't run in a loop, so the function is called randomly throughout the application.
- This isn't for a simulation, so "more or less" random is good enough.
Aucun commentaire:
Enregistrer un commentaire