How to return a random number between 0 and 199 with JavaScript?

To return a random number between 0 and 199, use the JavaScript Math.random() and Math.floor() methods together.

How It Works

Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive). To get integers from 0 to 199:

  • Multiply by 200 to get range 0 to 199.999...
  • Use Math.floor() to round down to nearest integer

Example

<!DOCTYPE html>
<html>
<body>
   <script>
      // Generate random number between 0-199
      let randomNum = Math.floor(Math.random() * 200);
      document.write("Random number: " + randomNum);
   </script>
</body>
</html>

Breaking Down the Formula

Let's see how Math.floor(Math.random() * 200) works step by step:

// Step 1: Math.random() returns decimal 0 to 0.999...
console.log("Math.random():", Math.random());

// Step 2: Multiply by 200 for range 0 to 199.999...
console.log("Math.random() * 200:", Math.random() * 200);

// Step 3: Math.floor() rounds down to integer
console.log("Final result:", Math.floor(Math.random() * 200));

// Generate multiple random numbers
for(let i = 0; i 

Math.random(): 0.7834592847361234
Math.random() * 200: 156.69185694722468
Final result: 156
Random 1: 43
Random 2: 187
Random 3: 12
Random 4: 199
Random 5: 0

Reusable Function

Create a function to generate random numbers in any range:

function getRandomNumber(min, max) {
   return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Generate numbers between 0 and 199
console.log("0-199:", getRandomNumber(0, 199));

// Test with other ranges
console.log("1-10:", getRandomNumber(1, 10));
console.log("50-100:", getRandomNumber(50, 100));
0-199: 156
1-10: 7
50-100: 73

Key Points

  • Math.random() never returns exactly 1, so multiplying by 200 gives 0 to 199.999...
  • Math.floor() ensures you get integers 0, 1, 2... up to 199
  • The range is inclusive of 0 and 199

Conclusion

Use Math.floor(Math.random() * 200) to generate random integers from 0 to 199. The formula multiplies the random decimal by the range size and rounds down to get clean integers.

Updated on: 2026-03-15T23:18:59+05:30

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements