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 −
Example
Live Demo
<!DOCTYPE html> <html> <body> <script> var max = 6 var min = 2 var random = Math.floor(Math.random() * (max - min + 1)) + min; document.write(random) </script> </body> </html>