How to get a random number between 0.0200 and 0.120 with JavaScript?

Sometimes, we want to get a random number between 0.0200 and 0.120 with JavaScript.

In this article, we’ll look at how to get a random number between 0.0200 and 0.120 with JavaScript.

How to get a random number between 0.0200 and 0.120 with JavaScript?

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.

For instance, we write

const n = (Math.random() * (0.12 - 0.02) + 0.02).toFixed(4);

to use Math.random() * (0.12 - 0.02) + 0.02 to return a number between 0.02 and 0.12.

Then we call toFixed with 4 to round the returned number to 4 decimal places.

Conclusion

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.