JS Coding Questions Logo
JS Coding Questions
#150💼 Interview💻 Code

How do you generate random integers

Advertisement

728x90

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,

javascript
1Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10
2
3Math.floor(Math.random() * 100) + 1; // returns a random integer from 1 to 100

Note: Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 65

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
150of476