📜  Math.random javascript double - Javascript (1)

📅  最后修改于: 2023-12-03 15:17:33.482000             🧑  作者: Mango

Math.random() in Javascript

Introduction

In Javascript, Math.random() method returns a random double between 0 (inclusive) and 1 (exclusive). This method is often used in applications that require randomization, such as games or simulations.

Syntax
Math.random()

The random method does not require any arguments.

Example
// Generate a random number between 1 and 10
const randomNum = Math.floor(Math.random() * 10) + 1;

console.log(randomNum);

In the above example, Math.floor() function is used to round down the result of Math.random() * 10 to get an integer between 0 and 9. The + 1 ensures that the final result is between 1 and 10, inclusive.

Conclusion

Math.random() is a useful method in Javascript that can be used to generate random numbers. Its functionality can be extended by using other mathematical functions such as Math.floor().