Category: Vanilla Javascript

Added: 22nd of October 2018

Viewed: 2,855 times

Generate a random number between 1 and 5 using Math.floor and Math.random commands in Javascript

The Javascript code below generates a random number between 1 and 5

Create a new .html file on your desktop, and save the file as random.html
Open the document in your text editor and copy and paste the following code.

var random_number = Math.floor(Math.random() * 5 + 1);