Tutorials/Rotating by a random angle

Rotating by a random angle
Suppose you want to draw a shape rotated by a random angle -- let's say specifically by a random integer from 0 to 359. One approach would be to define 360 rules, each of which rotates the shape by a different angle:

That's a lot of rules to enter for just one effect! Fortunately, there's a better approach, based on the concept of a Search tree. The idea is first to rotate either not at all or by half a circle (i.e., either 0 or 180 degrees), then to rotate the result either not at all or by a quarter of a circle (i.e., either 0 or 90 degrees), then to rotate the result either not at all or by an eighth of a circle (i.e., either 0 or 45 degrees). At this point, we've rotated by one of 0, 45, 90, 135, 180, 225, 270, or 315 degrees -- eight possible angles from only six rules. So far, so good.

If we divide by two again we're going to start seeing non-integer angles. However, nothing says we have to keep dividing by two. For the next rotation we can rotate by one of {0, 15, 30} degrees, giving us any of 24 possible angles as illustrated below:



The last steps are to rotate by one of {0, 5, 10} degrees and finally by one of {0, 1, 2, 3, 4} degrees. In the end, it takes only 18 rules to rotate a shape by any of 360 unique angles! The rotation is also very fast: only seven rules are actually executed no matter what angle is finally chosen.

Here's a sample picture that demonstrates the preceding technique:



Note that this technique applies just as well to selecting random colors, random distances, random tower heights, etc. as it does to random angles.

The advantages of the binary-search technique over something like are The only real disadvantage is code length.
 * execution speed,
 * uniform randomness (i.e., each number is equally likely), and
 * the ability to bound the range of numbers selected.