Code generating unwanted '0'

I've written this piece of code

for (int i=0; i<array.length-2; i++)//assigning random numbers to the array

{

array[i] = ( (int)(Math.random() * lastNumber + 1));

}

And I do not want it to generate the number 0 but it does. Can somebody please tell me where I went wrong? I thought even if Math.random produces a 0 and * it with lastNumber, it would still + 1 in the end but I still keep getting the number 0 generated especially in my second last and last array space.

lastNumber is defined by the user.

Thanks in advance to all who extend their assistance.>

[800 byte] By [JuppieJuxa] at [2007-10-3 6:15:16]
# 1

if you don't want the last two values in the array to be 0, then why is your termination condition i < array.length - 2?

If the user specifies lastNumber as a negative number then it will be possible to generate 0. So you should check to make sure the number they enter is >= 0.

rkippena at 2007-7-15 0:59:31 > top of Java-index,Other Topics,Algorithms...
# 2
Oops..Thank you..
JuppieJuxa at 2007-7-15 0:59:31 > top of Java-index,Other Topics,Algorithms...
# 3
also protect yourself from overflow - not likely but possible
marlin314a at 2007-7-15 0:59:31 > top of Java-index,Other Topics,Algorithms...
# 4
I would also do this:((Math.random() * lastNumber) + 1)); to make sure it does what you expect. I don't know whether this matters here or not and I don't care to look it up. It's bad practice to rely on the order of operations when the result isn't obvious.
dubwaia at 2007-7-15 0:59:31 > top of Java-index,Other Topics,Algorithms...