Generating Random Number in JavaScript
Generating random numbers is very important in Javascript like other programming languages… JavaScript adds dynamism to our html pages. For example: If you want to add a slideshow that is arranged in order at random to your page, you need to generate a random number. In this article, we will see to generate a random number by using Javascript.
Structure 1.0: This is basic statement and we don’t determine nothing like maximum or minimum limit. A float number is generated which falls between 0 and 1.
Example 1.0: Let’s get output.
Probable Output of Example 1.0:
1.1) Generating a Random Number Between Two Numbers
Structure 1.1: If we want to generate a random number between two numbers (that we determined), we should use this structure:
Formula: MinNum <= result < MaxNum
In this usage, the random number falls between MinNum(included) and MaxNum(excepted).
Example 1.1: We are generating a random number between 10(included) and 100(inculded).
Structure 1.2: This stucture can be used instead of Structure 1.1. The both structures do the same work except a little difference. The difference is using different functions in each structure which are floor() and round() functions.
Formula: MinNum <= result <= MaxNum
In this usage, the random number falls between MinNum(included) and MaxNum(included).
Example 1.1: We are generating a random number between 10(included) and 100(inculded).
Application 1.0 (Getting a random image from an array)
functions.js:
index.html:
Output of Application:
Download This Application:
RandomImage.rar (866 byte)
Recently Typed