The value of this expression will always be zero:
(int) Math.random()

Casting to an integer removes the fractional portion. Casting has a
higher operator precedence than multiplication or addition, so happens
first in your full expression. Parentheses could be used to alter when
the cast happens, however. Like this:
(int) (Math.random() * 9) + 1

On Nov 18, 11:00 am, Jags <[email protected]> wrote:
> I am fairly new to android and java
>
> i tried following code to generate different random numbers in the
> ranges of 1 - 9
>
> i wrote following code
>
>                                 int i = (int) Math.random() * 9 + 1;
>                                 while (i == previousIndex) {
>                                         i = (int) Math.random() * 9 + 1;
>                                 }
>
> always i is getting 0 and goes to infinite loop, why so ?
>
> sorry for a novice question though :P

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to