aspose file tools*
The moose likes Beginning Java and the fly likes Math.random() questions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Math.random() questions." Watch "Math.random() questions." New topic
Author

Math.random() questions.

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 89


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
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 89
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

Joined: Jun 18, 2012
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

Joined: Oct 24, 2010
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8186
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40

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

 
 
subject: Math.random() questions.