1

Trying to run random colors from the three defines color properties,

int num1 = rand() %3;
color1= [UIColor colorWithRed:0.5/num1 green:0.7/num1 blue:0.5/num1 alpha:1.0];
[self setColor:color1 setLabel:label1];

int num2 = rand() %3;
color2 = [UIColor colorWithRed:0.5/num2 green:0.7/num2 blue:0.5/num2 alpha:1.0];
[self setColor:color2 setLabel:label2];

int num3 = rand() %3;
color3= [UIColor colorWithRed:0.5/num3 green:0.7/num3 blue:0.5/num3 alpha:1.0];
[self setColor:color3 setLabel:label3];

trying to generate random colors from the color object, label object.

Want to put random() for the entire color variable not just for the properties. How can i do that. Thanks in advance

user2038249
  • 31
  • 1
  • 5

4 Answers4

2

You should change rgb values randomly....

color1= [self getRandomColor];                
[self setColor:color1 setLabel:firstColorObject];
color2 = [self getRandomColor];;
[self setColor:color2 setLabel:secondColorObject];
color3= [self getRandomColor];
[self setColor:color3 setLabel:thirdColorObject];

The method definition:

-(UIColor *)getRandomColor{
    UIColor *color;
    float randomRed = rand()%3;//3:you can write any number as you wish...
    float randomGreen =rand()%2;//2:you can write any number as you wish...
    float randomBlue =rand()%4;//4:you can write any number as you wish...
    color= [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
    return color;
}
Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • I am getting 9 different colors, when i set random()%3;, but i need only 3 colors randomly moving along 3labels – user2038249 Mar 05 '13 at 07:58
2

EDIT: (For ios)

-(NSDictionary *)randomColorAndLabel{
    UIColor *color1= [UIColor colorWithRed:0.2 green:0.4 blue:0.3 alpha:1.0];
    UIColor *color2= [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1.0];
    UIColor *color3= [UIColor colorWithRed:0.4 green:0.4 blue:0.3 alpha:1.0];

    NSDictionary *colorDict1=@{@"color1" : color1};
    NSDictionary *colorDict2=@{@"color2" : color2};
    NSDictionary *colorDict3=@{@"color3" : color3};

    NSArray *colors=@[colorDict1, colorDict2, colorDict3];

    NSInteger randomNumber=arc4random()%3;

    return colors[randomNumber];
}

which returns dictionary with color and name that you can use for label.


Earlier solved for OSX

I solved for OSX, with NSColor instead of UIColor and method name is changed as

colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];

-(NSDictionary *)randomColorAndLabel{
    NSColor *color1= [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.3 alpha:1.0];
    NSColor *color2 = [NSColor colorWithCalibratedRed:0.3 green:0.4 blue:0.3 alpha:1.0];
    NSColor *color3= [NSColor colorWithCalibratedRed:0.4 green:0.4 blue:0.3 alpha:1.0];

    NSDictionary *colorDict1=@{@"color1" : color1};
    NSDictionary *colorDict2=@{@"color2" : color2};
    NSDictionary *colorDict3=@{@"color3" : color3};

    NSArray *colors=@[colorDict1, colorDict2, colorDict3];

    NSInteger randomNumber=arc4random()%3;

    return colors[randomNumber];
}

this returns dictionary with name and color.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • how do i declare the NSColor identifier , it's saying not a valid declaration. Thanks – user2038249 Mar 05 '13 at 08:06
  • @user2038249: As i said it is coded for osx. read the first two lines of answer. Change `NSColor` to `UIColor` and change `colorWithCalibratedRed:` to `colorWithRed:` – Anoop Vaidya Mar 05 '13 at 08:13
  • Hi Anoop : why i am getting this error NSDictionary *colorDict2=@{@"color2" : color2};Expected ';'after top level declator' – Dipendra Mar 05 '13 at 08:42
  • NSDictionary *colorDict2=@{@"color2" : color2}; ERROR Expected ';'after top level declarator' – Dipendra Mar 05 '13 at 08:45
  • what about in `NSDictionary *colorDict1=@{@"color1" : color1};` ? comment next 2 lines and check. – Anoop Vaidya Mar 05 '13 at 08:47
  • @AnoopVaidya: I've updated the code, trying to make it simple, using the color properties. Please let me know if i could random the color variable not just each red,green,blue color property. – user2038249 Mar 07 '13 at 17:00
1
-(UIColor*) randomColor {
    CGFloat r = (float)(arc4random()%256) / 255.f;
    CGFloat g = (float)(arc4random()%256) / 255.f;
    CGFloat b = (float)(arc4random()%256) / 255.f;

    return [UIColor colorWithRed:r green:g blue:b alpha:1.f];

}
Alex_Burla
  • 800
  • 1
  • 9
  • 9
1

Finlay I found solution for random colour

Just use below code

- (UIColor *)getRandomColor {

    srand48(arc4random());

    float red = 0.0;
    while (red < 0.1 || red > 0.84) {
        red = drand48();
    }

    float green = 0.0;
    while (green < 0.1 || green > 0.84) {
        green = drand48();
    }

    float blue = 0.0;
    while (blue < 0.1 || blue > 0.84) {
        blue = drand48();
    }

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}

Please check reference of colour enter image description here

Vivek
  • 4,916
  • 35
  • 40