#### Archived

This topic is now archived and is closed to further replies.

# [java] Random colors?

This topic is 6103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering how I can create random color values for text, the only way I know how to set Text color in Java right now is c.setTextColor(Color.blue); or whatever, any help on how to create RGB values and store them as colors?

##### Share on other sites
  int red = (int)Math.floor(Math.random() * 255);int green = (int)Math.floor(Math.random() * 255);int blue = (int)Math.floor(Math.random() * 255);Color randomColor = new Color(red, green, blue);

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool

##### Share on other sites
Hmm... That''s a little off. You should be multiplying by 256, not 255.

But it''s also excessively complicated...
  java.util.Random r = new java.util.Random();Color randomColor = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256);

##### Share on other sites
No, actually... it is by 255. It returns a number 0.0 - 1.0... so if you multiply by 256, you can get an integer value of 256. Colors range from 0 -> 255... so by multiplying by 255, you will get a correct value.

Billy

##### Share on other sites
Ok, c_wraith, you got me on the 255/256 issue. Sorry AP, but he''s right, Math.random() returns >= 0.0 && < 1.0.

However, I use the Math.random() functions not because they''re visually cleaner, but programmaticly cleaner.
If you''re writing a game and every time you need a random number (which can be fairly often depending on the game) you create a new Random object, you''re incurring an unnessicary object creation, as well as giving more work to the garbage collector. You could use a static Random instance to avoid this, but then you''ve just effectivly duplicated what Math.random() does for you.
Also, if the int you pass in to Random.nextInt() is not a power of 2 it can be signifigantly slower than Math.random().

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool

##### Share on other sites
Well, yes, the java.util.Random should only be created once (per thread, anyway). However, I''d say it''s better than using Math.random(), for two reasons.

First off, if you create one per thread, you can get away without the synchronization Math.random() does. There''s one speed increase, and potentially a big one if you have several threads calling Math.random() simultaneously. Also, calling nextInt(int) may be slower than the call to nextDouble() that Math.random() makes, but there is one less level of indirection, and it doesn''t require floating point arithmetic and integer<->fp conversions. In other words, I''m not convinced Math.random() is any faster when looking for an integer result.

Second, nextInt(256) is more readable than the equivalent Math.random() based expression.

##### Share on other sites
Ok, I did some testing... Calling nextInt (even with a non-power of two argument) takes about half as long as calling Math.random() and converting it.

In other words, I stand by my recommendation of calling nextInt, as long as only one Random is created per thread.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631788
• Total Posts
3002356
×