2 Replies - 522 Views - Last Post: 07 October 2012 - 04:25 PM Rate Topic: -----

#1 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

create a random color from range.

Posted 07 October 2012 - 03:36 PM

I'm suppose to create a random color from the ranges:
0 <= red < 1.0
0 <= green < 1.0
0 <= blue < 1.0

Am I doing it right and if so is this the best way to do this?
//Random Object color
            float red   = (float) (Math.random() * (1.0));
            float green = (float) (Math.random() * (1.0));
            float blue  = (float) (Math.random() * (1.0));
            Color color = new Color(red, green, blue);



Is This A Good Question/Topic? 0
  • +

Replies To: create a random color from range.

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: create a random color from range.

Posted 07 October 2012 - 04:20 PM

Well your way does work, but the random method from the Math class already returns a value in the range 0=< Math.random() < 1

So adding the *1.0 isn't necessary.
Was This Post Helpful? 1
  • +
  • -

#3 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: create a random color from range.

Posted 07 October 2012 - 04:25 PM

Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1