# Math.random() questions.

WeiJie Lim
Ranch Hand
Posts: 92

In this case, the Math.random() method will give me a value of 0 as its value is being casted to int, which means the decimal values are removed ?

Which doesn't make sense.

Any guidance is appreciated. =)

Henry Wong
author
Marshal
Posts: 21497
84
WeiJie Lim wrote:

In this case, the Math.random() method will give me a value of 0 as its value is being casted to int, which means the decimal values are removed ?

A double multiplied with an int expression results in a double value. And yes, when this resultant double value gets cast to an int, the resultant is an int value.... however, I don't know how you know that the resultant int will be zero, as that will require that you know that the resultant double was less than one.

Henry

WeiJie Lim
Ranch Hand
Posts: 92
Henry Wong wrote:

A double multiplied with an int expression results in a double value. And yes, when this resultant double value gets cast to an int, the resultant is an int value.... however, I don't know how you know that the resultant int will be zero, as that will require that you know that the resultant double was less than one.

Henry

It is because Math.random() gives the random result of 0.0 inclusive to 1.0 exclusive ?

So if the decimal value is shaved away due to being casted to int , wouldnt it be always be 0 ?

Sorry if I have some weird way of thinking..

Phil English
Ranch Hand
Posts: 62
WeiJie Lim wrote:
Henry Wong wrote:

A double multiplied with an int expression results in a double value. And yes, when this resultant double value gets cast to an int, the resultant is an int value.... however, I don't know how you know that the resultant int will be zero, as that will require that you know that the resultant double was less than one.

Henry

It is because Math.random() gives the random result of 0.0 inclusive to 1.0 exclusive ?

So if the decimal value is shaved away due to being casted to int , wouldn't it be always be 0 ?

Sorry if I have some weird way of thinking..

The key to this is when the cast to int is made. In this case it is made on the contents of the proceeding parentheses, i.e. after you have multiplied by your range so it isn't casting a value from 0-1 it is casting a value from 0-5 (in this case).

This might be helpful: http://www.cis.upenn.edu/~palsetia/java/precedenceTable.html

Just try it - you'll find it works fine with a bit of tidying up.

Greg Brannon
Bartender
Posts: 563
I don't understand the source of confusion in your original post. It seems you basically understand the mechanics of the parts involved but are (maybe) confused or doubting your understanding by some observation you've made, but the observation isn't clear. What results did you observe from the posted code that were unexpected or inconsistent with your understanding?

Well, after rereading your original post, I'll modify my statement some, because your statement, "In this case, the Math.random() method will give me a value of 0," is incorrect, so let's start over with a better understanding of the source of your confusion.

Winston Gutkowski
Bartender
Posts: 10527
64
WeiJie Lim wrote:In this case, the Math.random() method will give me a value of 0 as its value is being casted to int, which means the decimal values are removed ?
Which doesn't make sense.
Any guidance is appreciated. =)

The key is in what Math.random() returns. Specifically, from the docs:
"a pseudorandom double greater than or equal to 0.0 and less than 1.0."

So:
1. What happens when you multiply that value by N?
2. What happens when cast that result to an int?

However, once you've actually worked it out for yourself, my suggestion would be to forget it. There's a MUCH better alternative: java.util.Random.
For one thing it has methods to return pretty much any type you want.
Secondly, Math.random() has been retro-fitted to use java.util.Random under the hood, so if you use it, you're simply running Random.nextDouble() anyway.

Winston

Campbell Ritchie
Sheriff
Posts: 50189
79
If you want an int, why not use a method in the Random class which returns an int? You will probably find this makes your programming much much easier.

Henry Wong
author
Marshal
Posts: 21497
84
• 1
WeiJie Lim wrote:
Sorry if I have some weird way of thinking..

It is not a "weird way of thinking", as we have all done it. It is likely a case of being so focused on one point, that you forgot everything else.... and as everyone already pointed out, so focused on the fact that a cast of a double value of between zero and less than one will yield zero, that you forgot the the random value is within a larger expression that is within a set of parenthesis, and the cast is outside those parenthesis.

Henry