/ Published in: JavaScript
Simple function to return a value between the minValue and the maxValue.
Expand |
Embed | Plain Text
// Random integer between function randomIntegerBetween(minValue,maxValue){ return Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue; }
Comments
Subscribe to comments
You need to login to post a comment.

Math.min is unnecessary but doesn't actually hurt the calulation.
Math.round, on the other hand, skews the numbers heavily away from the min and max values, you'll hardly ever get them.
After some quick testing, this seems to work much better:
Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue;Thanks for the feedback mwalsh! Like most code, this was written out of utility, and it's very cool of you to help me refine it. I've updated the snippet. Thanks!