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