Random Color random(255) or random(256)?

edited January 2018 in Programming Questions

I am trying to make a random color in processing. Online I found two solutions.

SOLUTION_A: color c=color(int(random(255)),int(random(255)),int(random(255)));

SOLUTION_B: color c=color(int(random(256)),int(random(256)),int(random(256)));

The difference being that SOLUTION_A uses random(255) and SOLUTION_B uses random(256)

What solution is correct?

I think SOLUTION_B correct but I'm not sure.



  • edited January 2018 Answer ✓

    If you're removing the fractional part of the random()'s value, either via PApplet::int() or casting w/ (int) or (color), the correct argument for a 0 to 255 range is 256. :-B

  • A much shorter version for all possible 100% opaque colors: (color) random(#000000); :ar!

  • edited January 2018

    @GoToLoop Thanks for the answer!!! It confirms what I thought. And thanks for the shortcut, it should come in handy.

  • @GoToLoop nice one, I wondered for a long time what a nice way would be instead of the color(random(), random(), random()).

Sign In or Register to comment.