Sto cercando di ottenere un colore casuale. L'ho fatto usando la forza bruta ma questo metodo sembra eccessivamente laboriosa (anche se la distribuzione è abbastanza ancora):getRandomColor ma evita i colori scuri: aiuta il mio algoritmo
- (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];
}
Come può essere migliorato? Voglio una distribuzione uniforme di rosso, verde e blu e niente di troppo scuro (altrimenti avrei preso tre numeri casuali e avrei finito con esso).