This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Trevor I think you want to add 3 to it to get a number from 3 to 8 because random return a number >= 0 and <1. Dave
Joined: May 30, 2001
yep, quite right. Maths let me down there. The key concept is (int)2.999999 = 2. Casting to an int always rounds down.
Joined: May 03, 2001
Hi Trevor & Dave. I mean, can we make a generalized formula for generating numbers with our rquired range.Something like this. (int)(Math.random() * lower limit )+ upper limit.. where lower limit is in my first post is 3 & upper limit is 8. That is have a generalized formula in which i have to just put lower & upper limts. Regards, Hassan.