You can use Math.random() with Math.floor() to return random integers. For example, if you want generate random integers between 1 to 10, the multiplication factor should be 10,
1 2 3 4 5 | // returns a random integer from 1 to 10 Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 100 Math.floor(Math.random() * 100) + 1; |
Note: Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.