How to generate random whole numbers in JavaScript in a specific range?

To generate a random number, use the JavaScript Math.random() method. Here set the minimum and maximum value and generate a random number between them as in the following code −


Live Demo

<!DOCTYPE html>
         var max = 6
         var min = 2
         var random = Math.floor(Math.random() * (max - min + 1)) + min;