KnowledgeBoat Logo
|

Computer Applications

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.

Java Math Lib Methods

22 Likes

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

Answered By

9 Likes


Related Questions