JavaScript Generate Random Integer From Min and Max

Apr 16, 2020
function getRandomInt(min, max) {  return Math.floor(Math.random() * ((max+1) - min)) + min}

Usage

getRandomInt(0,0)   // always 0getRandomInt(0,1)   // 0-1getRandomInt(0,10)  // 0-10getRandomInt(1,10)  // 1-10

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.