-1

This piece of code is giving the result I wanted

double a=Math.random()*100;
int b=(int)a;
System.out.println(b);

but when I did for the same thing this way, it is giving zero always

int a=(int)Math.random()*100;
System.out.println(a);

As far as I know typecasting will store the bigger data-type to small. but in the later code, I'm gettig zero every single time I run the program.

Daniel
  • 1,861
  • 1
  • 15
  • 23

7 Answers7

5

(int)Math.random()*100; means:

cast the value of 'Math.random()' to int and then multiple by 100. Since Math.random returns a number between 0 and 1 casting to int will always chop off the decimal path and give you 0.

The correct way to do it is

(int)(Math.random()*100);

Nazgul
  • 1,892
  • 1
  • 11
  • 15
2

Try with,

int a=(int)(Math.random()*100);
System.out.println(a);

Math.random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
2

Type casting is both right-to-left associative, and has a higher precedence than multiplication.

Source

Let me break it down a bit.

Like any other operator, a type cast has a certain level of priority attached to it. Certain operators have higher priority than others, and certain operators associate with their arguments in a certain way. For example, numerical operations are typically left-to-right associative; that is, with operators of an equal priority, one can parenthesize them from the left and get the same result.

Example:

5 * 5 * 5 / 3 + 5 -> (((5) * 5) * 5) / 3) + 5

Right-to-left associativity would allow you to parenthsize the operations from the right to achieve the same result.

Example:

int x;
int y;
int z;
x = y = z = 10 -> x = (y = (z = (10))

Type casting, much like assignment, is right-to-left associative, so it's going to essentially use the rightmost argument to satisfy its closest association, then progress backwards through the chain until you reach the ultimate goal.

This means that you could write this as valid syntax, although it would be very strange:

Object foo = (Object)(Number)(float)Math.random();

First, we cast our result from a double to a float, then to a Number, then to an Object (which is totally redundant, but possible).

The priority bit in particular is what causes the multiplication to apply last in this chain of events.

Bear in mind, in no one level of priority does the language intermingle associativity. That means an operator at level 1 priority associates in a specific way, and operators in level 2 operate in a specific way.

So, we come now to your expression:

int a = (int) Math.random() * 100;

Casting has a priority level of 3. The mathematical operator * has a priority level of 4. So, Java sees your statement like this:

int a = ((int) Math.random()) * 100;

The parentheses are there for extra clarity. (Note that parentheses have the highest priority.)

What you want to do, as explained more tersely in other answers, is use the parentheses to force your intended priority instead of letting Java decide. So, you would write this instead:

int a = (int) (Math.random() * 100);

This forces Java to evaluate the multiplication before the cast, since the parentheses have a higher priority than the cast.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Because the cast to int happens before the multiplication and as Math.random() generates a number between 0 (inclusive) and 1 (exclusive), it is rounded to 0.

Smutje
  • 17,733
  • 4
  • 24
  • 41
1

You are casting before multiplying here... Math.random() will generate number between 0 and 1 and will be rounded to 0 before multiplying with 100.

(int)(Math.random()*100) will behave as you want.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
1

Consider using java.util.Random:

Random random = new Random();
int value = random.nextInt(100);
System.out.println(value);
  • Random is thread-safe, whereas Math.Random() is not.
  • When testing, you can feed a Random the same seed to generate reproducible pseudorandom numbers. You can't with Math.Random().
  • and more.

See more here: Math.random() versus Random.nextInt(int)

Community
  • 1
  • 1
jdphenix
  • 15,022
  • 3
  • 41
  • 74
0

use this :

int a=(int)(Math.random()*100);
System.out.println(a);
Scrooge McD
  • 335
  • 1
  • 3
  • 12