How to generate a random number between two numbers in JavaScript
New Courses Coming Soon
Join the waiting lists
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.
→ Get my JavaScript Beginner's Handbook
→ Read my
JavaScript Tutorials
on The Valley of Code
→ Read my
TypeScript Tutorial
on The Valley of Code
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- THE VALLEY OF CODE your web development manual
- BOOKS 17 coding ebooks you can download for free on JS Python C PHP and lots more
- Interesting links collection
- Follow me on X