What is the purpose of the Math.random() method in JavaScript?
a) To generate a random number between 0 and 1
b) To round numbers to the nearest integer
c) To generate a random integer
d) To find the square root of a number
Answer:
a) To generate a random number between 0 and 1
Explanation:
The Math.random()
method generates a pseudo-random number between 0 (inclusive) and 1 (exclusive). It is often used in combination with other functions to generate random numbers within a specific range.
Example:
let randomNum = Math.random();
console.log(randomNum); // Output: A random number between 0 and 1