# Errata: Scaling factor for UIColor in Detecting Shakes secti

#1

In the `motionBegan:withEvent:`

`random() % 256` will return values between 0 and 255. Therefore the scaling factor to generate corresponding float values between 0 and 1.0 is 255.

``````float r, g, b;
r = random() % 256 / 255.0;
g = random() % 256 / 255.0;
b = random() % 256 / 255.0;``````

#2

[quote=“falconcreek”]In the `motionBegan:withEvent:`

`random() % 256` will return values between 0 and 255. Therefore the scaling factor to generate corresponding float values between 0 and 1.0 is 255.

``` float r, g, b; r = random() % 256 / 255.0; g = random() % 256 / 255.0; b = random() % 256 / 255.0; ```[/quote]

I think it boils down to operator precedence so that 256 / 256.0 gets evaluated first, resulting in the expression random() % 1.0, which in turn generates the desired float values between 0 and 1.0

So unless I’m mistaken, the expression is interpreted as: random() % (256/256.0) instead of: (random() % 256) / 256.0

#3

Hi,

The operator precedence for % and / would just be left to right so falconcreek is correct.

Bear in mind that we are talking about a ~0.004 discrepancy so if you can spot the difference you should be spending your time looking for signs of extraterrestrials…

Gareth

#4

[quote=“GarethR”]Hi,

The operator precedence for % and / would just be left to right so falconcreek is correct.

Bear in mind that we are talking about a ~0.004 discrepancy so if you can spot the difference you should be spending your time looking for signs of extraterrestrials…

Gareth[/quote]

Ah ok, I guess I was confusing the role of the modulus operator in that expression. I always thought that when used with random(), the % acted as a limiting factor, for example “random() % 256” limiting the results to numbers between 0 and 255. So when I saw “random() % 256/256.0” I just assumed it meant “Divide the integer 256 with the float 256.0 to get the float 1.0, and then generate a random number limiting the results to values between 0 and 1.0”

When I mentioned precedence earlier, I wasn’t considering the modulus as an arithmetic operator, in which case the % and / have equal priority and the expression would be evaluated from left to right as you mentioned. The lack of parentheses makes it rather confusing, and little details like this always distract me from focusing on the big picture…thanks for clearing that up!