How to generate a random number between two numbers in JavaScript
The simplest possible way to randomly pick a number between two
Use a combination of Math.floor()
and Math.random()
.
This simple one line of code will return you a number between 1 and 6 (both included):
Math.floor(Math.random() * 6 + 1)
There are 6 possible outcomes here: 1, 2, 3, 4, 5, 6.
→ Here's my latest YouTube video
→ Get my JavaScript Beginner's Handbook
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025