This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Random Colors???HELP!

Annette Pomeroy
Greenhorn
Posts: 2
I was wondering if anyone could tell me which method would i use to randomly generate colors, when i am not sure how many times i need it generated.

Rowan Brownlee
Ranch Hand
Posts: 97
Hi Annette,
The closest I've gotten to this is setting up an array of colours and cycling through them over and over. Though this isn't random, I suppose you could repeatedly generate a random number between 0 and the number of colours in the array to choose a colour at random.
I haven't tried to generate customised colours, but looking at the constructors in the Color class, it looks like you can feed in your own (randomly generated )numbers to make a colour. For generating random numbers, there's the Random class, and Math.random.
Constructor example from Color API
~~~~~~~~~
Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
~~~~~~~~~
cheerio
rowan

Annette Pomeroy
Greenhorn
Posts: 2
Thanks for your help, I appreciate it much!
I will try what u have told me.
Thanks again,
Annette

Rikard Qvarforth
Ranch Hand
Posts: 107
maby you could do something like this ..
Color ColorTest(){
int R = 0;
int G = 0;
int B = 0;
R = (int)(1+Math.random()*(255));
G = (int)(1+Math.random()*(255));
B = (int)(1+Math.random()*(255));
return new Color(R,G,B));
}
hope it helps ..