Javascript Program to Generate a Random Number

To understand this example, you should have the knowledge of the following JavaScript programming topics:


In JavaScript, you can generate a random number with the Math.random() function.

Math.random() returns a random floating-point number ranging from 0 to less than 1 (inclusive of 0 and exclusive of 1)


Example 1: Generate a Random Number

// generating  a random number
const a = Math.random();
console.log(a);

Output

0.5856407221615856

Here, we have declared a variable a and assigned it a random number greater than or equal to 0 and less than 1.