# Math.random() !!!!!!

sunil kathuria

Greenhorn

Posts: 24

posted 15 years ago

this following question is from mock exam by RHE

what is the value of the follwing expression ?

MAth.round(Math.random()+2.50001);

options are :-

A. 2

B. 3

C. It is impossible to say

the answer is b ie. ->3

my question is that when Math.random() method generate

a random no. say 0.99999 on addition with 2.50001 it

becomes 3.50 and after rounding the result will give

4 which is not in the option list . so what is the

right answer

please tell me the right answer

thanks in advn.

sunil

what is the value of the follwing expression ?

MAth.round(Math.random()+2.50001);

options are :-

A. 2

B. 3

C. It is impossible to say

the answer is b ie. ->3

my question is that when Math.random() method generate

a random no. say 0.99999 on addition with 2.50001 it

becomes 3.50 and after rounding the result will give

4 which is not in the option list . so what is the

right answer

please tell me the right answer

thanks in advn.

sunil

sandip karmarkar

Greenhorn

Posts: 26

Mihai T

Greenhorn

Posts: 4

posted 15 years ago

Hi,

You are right: the expression could be evaluated to 3 or 4. The answer b (i.e. 3) is wrong.

Try this two or three times:

public class Z {

public static void main(String[] args) {

int j = 0;

for (int i = 0; i < 10000;i++) {

j = (int)Math.round(Math.random() + 2.50001);

if (j != 3) System.out.println("i = " + i + " j = " + j);

}

}

}

Mihai T.

[This message has been edited by Mihai T (edited June 25, 2001).]

You are right: the expression could be evaluated to 3 or 4. The answer b (i.e. 3) is wrong.

Try this two or three times:

public class Z {

public static void main(String[] args) {

int j = 0;

for (int i = 0; i < 10000;i++) {

j = (int)Math.round(Math.random() + 2.50001);

if (j != 3) System.out.println("i = " + i + " j = " + j);

}

}

}

Mihai T.

[This message has been edited by Mihai T (edited June 25, 2001).]

V Srinivasan

Ranch Hand

Posts: 99

posted 15 years ago

Hi Sunil, sandip & Mihai,

The result is just because of Math.round. Math.round method always casts to either int or long and returns.

What JLS says about Math.round :

The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int.

In other words, the result is equal to the value of the expression:

(int)Math.floor(a + 0.5f) or (long)Math.floor(a + 0.5d)

Hope this helps.

Thanks and regards

V.Srinivasan

The result is just because of Math.round. Math.round method always casts to either int or long and returns.

What JLS says about Math.round :

The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int.

In other words, the result is equal to the value of the expression:

(int)Math.floor(a + 0.5f) or (long)Math.floor(a + 0.5d)

Hope this helps.

Thanks and regards

V.Srinivasan

Manfred Leonhardt

Ranch Hand

Posts: 1492

posted 15 years ago

Hi Sunil,

As you have mentioned, the book answer is incorrect. The correct answer is

Find the errata here for RHE book.

Errata for Complete Javaï¿½ 2 Certification Study Guide

Regards,

Manfred.

As you have mentioned, the book answer is incorrect. The correct answer is

**C**. It has been corrected in other printings. When you buy a book, always look for the Errata on-line because all books have errors!Find the errata here for RHE book.

Errata for Complete Javaï¿½ 2 Certification Study Guide

Regards,

Manfred.