Re: How to use "Math.random()" to generate a random number in JavaScript?
If you want to generate the random numbers from a given range, follow the steps for that :
- First you will have to get the number between 0 and 10 from random() method.
- After that you will have to multiply that number with the difference of upper value and one less than the lower value of the range.
- For converting that into an integer. you will have to use floor().
- Lastly add the lower value of the range.
Check the following example :
Code:
var rand_no = Math.floor((20-9)*Math.random()) + 10;
alert(rand_no);
The code above generates a random number between 10 and 20.
IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....
Bookmarks