IMG-LOGO

How to generate random number in Javascript?

andy - 23 Dec, 2013 3082 Views 0 Comment

This tutorial will show you quickly how you can generate random number in Javascript.

The syntax to generate a random number is by using the following function:

Math.random()

Example of returning a random number between 1 and 100:

Math.floor((Math.random()*100)+1);

You can as well create a simple javascript function to accept minimum and maximum number to be generated. Here is the sample function:

function generateRandomNumber (minNumber, maxNumber) {
    return Math.floor(Math.random() * (maxNumber - minNumber + 1)) + minNumber;
}

To use above sample function:

generateRandomNumber(100,1000);

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles