<< View More Computer Tips

Added On: 22/10/18   |    Category: Vanilla Javascript    |    Number of Views: 429

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

The code below generates a random number between 1 and 5

Create a new .html document on your desktop, and rename the file example3.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>

Example 1: Using document write() in javascript
Example 2: Using document.getElementByID() in javascript to send form data
Example 3: Generate a random number between 1 and 5 using Math.floor and Math.random commands in Javascript

Search

Services

The Linux Foundation

Best selling Unix / Linux books
Nvidia Graphics Cards
Save 15% off .CO.UK domains at 123-Reg
Save 15% off .COM domains at 123-Reg
cPanel Hosting Plans
Xilo Web Hosting Plans