ICSE Class 10 Computer Applications Question 31 of 76

Revising Basic Java Concepts — Question 35

Back to all questions
35
Question

Question 16

A student incorrectly attempted to produce a random value in the range 1.6 using the expression.

6*(int)Math.random( ) + 1

Correct the error in expression above to get the desired result.

Answer

The correct expression to get the desired result is given below:

(int)(6 * Math.random( )) + 1

Explanation

The formula to get an integer number between 1 and n is:

int r = (int) (n * Math.random()) + 1