# Logic Help

• 01-22-2012, 08:16 AM
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:
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         }```
• 01-22-2012, 02:22 PM
Norm
Re: Logic Help
Quote:

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.
• 01-22-2012, 08:38 PM
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.
• 01-22-2012, 08:43 PM
Norm
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.
• 01-22-2012, 10:44 PM
CuppaCoffee
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.
• 01-23-2012, 12:16 AM