6 ms·
Off topic, but this is a code smell for me: [(int) Math.round(Math.random() * (userAgents.length - 1))]); This leads to a lower probability of selecting the 0th
by homami 5y ago
Off topic, but this is a code smell for me: [(int) Math.round(Math.random() * (userAgents.length - 1))]); This leads to a lower probability of selecting the 0th and the last items in the array.
- namdnay 5y agoIt should be floor right? Math.floor(Math.random()*userAgents.length)
- lern_too_spel 5y agoThere's a built-in method for randomly selecting an int in a range. Use that.
- csears 5y agoAre you thinking of the Lodash random() function? Or Crypto.getRandomValues()?
- lern_too_spel 5y agohttps://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int- https://docs.oracle.com/javase/8/docs/api/java/util/Random.h...
- homami 5y agoYeah. > The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0.
- lern_too_spel 5y agofloor is unnecessary. The cast does it for you. The problem is that a random double is not going to be evenly distributed into length equal parts unless length is a power of 2.