DEV Community

Discussion on: How do you do random?

Collapse
 
bgadrian profile image
Adrian B.G.

Others responded on-topic, so I would add an off-topic :)

I do not build a JS project without Lodash (or something similar), it covers the basic needs and lacks of JS, acting like a Standard library in other languages. A few of them are related to random

_.random([lower=0], [upper=1], [floating])

_.sample(collection) //get a random element
_.sampleSize(collection, [n=1])

But, statistically speaking, taking a float random and truncating it to just a small integer range it's like buying a Ferrari for commuting, just saying :D Most of the times is a waste of resources.

Devs should be more careful at the distributions, most of the cases the code will be used for a few values and a small range of values, which will lead to very not-so-random results.

If the random results will affect the User Experience you should think twice of using it, the screwed distribution will affect your sales and KPIs in unknown ways, most likely negative, and there are always better alternatives, that require more resources to implement of course.