Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:42+00:00 2026-05-16T03:34:42+00:00

I am trying to get a random color. I have done it using brute

  • 0

I am trying to get a random color. I have done it using brute force but this method seems overly laborious (though the distribution is pretty even):

- (UIColor *) getRandomColor {
 // GOAL: reject colors that are too dark
 float total = 3;
 float one = arc4random() % 256 / 256.0;
 total -= one;
 float two = arc4random() % 256 / 256.0;
 total -= two;
 float three = total; // UIColor will chop out-of-range nums

 NSMutableArray *threeFloats = [[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithFloat:one], [NSNumber numberWithFloat:two], [NSNumber numberWithFloat:three], nil] autorelease];

 NSNumber *red, *green, *blue;
 red = [threeFloats objectAtIndex:arc4random() % [threeFloats count]];
 [threeFloats removeObject:red];
 green = [threeFloats objectAtIndex:arc4random() % [threeFloats count]];
 [threeFloats removeObject:green];
 blue = [threeFloats lastObject];

 return [UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:1];
}

How can it be bettered? I want an even distribution of red, green and blue and nothing too dark (otherwise I’d grab three random numbers and be done with it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:34:42+00:00Added an answer on May 16, 2026 at 3:34 am
    + (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
    
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    

    If white is ok then do saturation like hue instead of brightness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a random cell in jquery, between 1-16, I have
I'm trying to get the ADT working in Eclipse, but I'm getting random hangs
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Just trying to get my head around Generics by reading this enlightening article by
Am trying to get a random photo from each album from the data created
I'm trying to get random values out of an array and then break them
I am trying to find a way to get a random selection from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.