1. Member
Join Date
Jan 2012
Posts
10
Rep Power
0

## Logic Help

I'm trying to make the program randomly choose between colors, but I also want the user to be able to select which colors it chooses from. I've been using a simple (int)Math.random()*4 to pick a random number assigned to a color, but when the user chooses to omit colors and there are gaps where no color can be assigned because the random number generated is no longer being used, it messes with the program. What I would like to know is, is there a better way of doing this while still using random or is there a better command that will fix the problem?

The bit of code in question:
Java Code:
```set = (int)(Math.random()*4);

if(set == 3 && activeRed == 1){
wColor = new Color(256,0,0);//red
}

if(set == 2 && activeGreen == 1){
wColor = new Color(0,256,0);//green
}

if(set == 1 && activeBlue == 1){
wColor = new Color(0,0,256);//blue
}```

2. ## Re: Logic Help

I'm trying to make the program randomly choose between colors, but I also want the user to be able to select which colors it chooses from.
Can you explain how you want to choose a color? When do you do it randomly and when does the user choose?

For example the user gives the program 6 colors and the program randomly chooses one of those six colors.

3. Member
Join Date
Jan 2012
Posts
10
Rep Power
0

## Re: Logic Help

It is set at choosing from red green and blue, but the user can choose to make it pick from any combination of the three. For example, user wants it to only choose between red and blue while completely ignoring green. The problem is that right now it can still get the number for green, meaning it picks no color at all.

4. ## Re: Logic Help

If the user wants a choice of two colors, then the random choice should only be between those two colors.
Make a list of the user's selections and chose from that list.

5. ## Re: Logic Help

You could also make it that if the color that's not wanted is chosen, the randomizer would simply randomize again. Example, the three colours are red, blue and green, but the user only wants the program choose red and blue. The program will begin randomizing. If the colour chosen is green, it will simply randomize again, and keep doing so until it is not green.

6. Member
Join Date
Jan 2012
Posts
10
Rep Power
0

## Re: Logic Help

Thanks for the suggestions you guys, got it working.

7. ## Re: Logic Help

What is supposed to happen when the random variable, set, is 0 which will happen 1 out of 4 times?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•