Java Number Generation within Constraints help*

Kasugi

Active Member
Joined
Mar 23, 2008
Messages
40
Reaction score
0
Location
I was just leaving, soz.
Okay, this is a really arb question 0_o but here it goes....

I'm trying to generate a number in the range of 1000 and 9000
* It must be a multiple of 5
* It must NOT be a multiple of 3
Like, for example, 1355.

thus far I just have the constraints of :
num = (int)(Math.random()*8991)+1000;

but then like.... what next ? :| I'm so confused :erm:
 
try

int num=(int)((((Math.random()*533)*3+1)*5)+1000);

I'll let you figure out the rest!

Oops. Edited thinking I don't need the one, then I realised I do. Need the one to ensure the integrity of the range of 1000-9000, and it must be 1 and not two, so that when I add 1000 it doesn't add up to three with the remainders.

This smacks of a homework assignment, hence the op hasn't actually denied it. The algorithm is too co-incidental with the number range.
 
Last edited:
Brute force anyone?

Code:
using java.util.Random 

private int GetRandom()
{
    Random r =	new Random();
    int number = 0;

    do
    {
         // Assuming your range in inclusive?
         number = r.nextInt(8001) + 1000;
    }
    while (((number % 5) == 0) && (number % 3 != 0));
    return number;   
}
;
 
Top
Sign up to the MyBroadband newsletter
X