In the following code, I'm creating a category on UIColor to create a random color. However, from this code, I would have thought that the 'if' conditional would be true every time the method is run, instead of just the first time.
I think I'm not really understanding static variables correctly. Does a static variable only get set once, and then the second time the method is run, that line is just ignored? (so seeded would forever be YES after the first run)?
@interface UIColor(Random)
+(UIColor *)randomColor
{
static BOOL seeded = NO;
if (!seeded)
{
seeded = YES;
srandom(time(NULL));
}
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}