Home | About | Categories | All Tips & Tutorials

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

ID: 108

Category: Vanilla Javascript

Added: 22nd of October 2018

Views: 3,328

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.

<script>
var random_number = Math.floor(Math.random() * 5 + 1);
document.write(random_number);
</script>