Question:

Write a JavaScript program that uses Math.random() to generate three random numbers greater than 10 and less than 100. The program should also display the sum of the numbers, the largest number, and the smallest number.
Note: Use built-in JavaScript functions wherever needed.

Show Hint

To test the boundary conditions of this scaling formula: - If Math.random() yields $0$: $0 \times 89 + 11 = 11$ (satisfies $> 10$). - If Math.random() yields $0.999$: $0.999 \times 89 + 11 = 88.91 + 11 = 99.91 \rightarrow \lfloor 99.91 \rfloor = 99$ (satisfies $< 100$).
Updated On: Jun 29, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Generate Random Numbers in the Required Range

The Math.random() function generates a random decimal number in the range:

\[ 0 \le r < 1 \]

We need random integers that are greater than 10 and less than 100, i.e.,

\[ 11 \le N \le 99 \]

The general formula for generating a random integer between Min and Max (inclusive) is:

\[ N=\left\lfloor \text{Math.random()} \times (\text{Max}-\text{Min}+1)\right\rfloor+\text{Min} \]

Here,

\[ \text{Min}=11,\qquad \text{Max}=99 \]

Therefore,

\[ \text{Range Size}=99-11+1=89 \]

\[ N=\left\lfloor \text{Math.random()} \times 89\right\rfloor+11 \]


Step 2: Find the Sum, Largest, and Smallest Numbers

After generating three random numbers:

  • Add them to calculate the sum.
  • Use Math.max() to find the largest number.
  • Use Math.min() to find the smallest number.

The built-in JavaScript methods are:

  • Math.max(a, b, c) → Returns the largest value.
  • Math.min(a, b, c) → Returns the smallest value.

Step 3: Complete JavaScript Program

<!DOCTYPE html>
<html>
<head>
    <title>Random Number Generator</title>
</head>
<body>

<h2>Random Number Calculations</h2>

<script>
// Generate three random integers between 11 and 99
var num1 = Math.floor(Math.random() * 89) + 11;
var num2 = Math.floor(Math.random() * 89) + 11;
var num3 = Math.floor(Math.random() * 89) + 11;

// Calculate sum
var sum = num1 + num2 + num3;

// Find largest and smallest values
var largest = Math.max(num1, num2, num3);
var smallest = Math.min(num1, num2, num3);

// Display results
document.write("Generated Numbers: " + num1 + ", " + num2 + ", " + num3 + "<br>");
document.write("Sum of Numbers: " + sum + "<br>");
document.write("Largest Number: " + largest + "<br>");
document.write("Smallest Number: " + smallest + "<br>");
</script>

</body>
</html>

Sample Output

Generated Numbers: 25, 78, 41 Sum of Numbers: 144 Largest Number: 78 Smallest Number: 25

Was this answer helpful?
0
0