How to generate a random number in JavaScript
Monday, May 25, 2026
Generating random numbers is fundamental for games, animations, data sampling, color generation, and probabilistic UI behavior in JavaScript applications.
With over 25 years of experience in software development and as the creator of CoreUI, I’ve used Math.random() extensively in chart demos, color pickers, and interactive examples.
The built-in Math.random() function is the foundation for all client-side random number generation — simple, universally supported, and sufficient for most UI use cases.
For integers specifically, see how to generate a random integer in JavaScript.