Craig Lindley is a degreed hardware engineer who, until recently, had been writing large-scale Java applications. Now in retirement, he designs, builds and writes about the projects he wants to. He can be contacted at [email protected]
We programmers usually strive to write deterministic code that has predictable, repeatable behavior. Behavior that is not predictable and repeatable usually indicates a bug that must be found and fixed. Sometimes, however, the introduction of controlled randomness may be exactly what is needed to make our presentations more dynamic or to better test software we have already written.
In my case I was writing a ray tracing application for the iPhone/iPod Touch called Art Rays Lite. This app generates unique, one of a kind, ray-traced images in real time. Initially I used unconstrained randomness to select which geometric shapes (spheres, cones, cylinders, boxes, planes) to use within a scene, where these shapes would be placed in the 3D space, what their surface characteristics were (dull, shiny, reflective, etc.) and what colors would be used for these objects and for the scene light sources.
While the images produced were almost always interesting in some aspect the random selection of colors produced images that were sometimes jarring and not altogether pleasant. Since I actually wanted to sell this app it was important that most of the generated images were interesting and pleasant to look at.
This led me to the concept of using palettes for color selection instead of randomly generating colors. In this context a palette is a collection of 256 related colors. I decided to define numerous palettes because I wanted a lot of variation in the generated images. To this end I came up with the set of palettes in Table 1 for use in my ray tracer.
I started to formulate the problem as follows: I would like to use the R_GRAYSCALE palette approximately X percent of the time, the R_COLOR palette approximately Y percent of the time, the R_COLOR_FROM_IMAGE palette Z percent of the time and so on. The ProbabilitySelector class was developed to encapsulate this probability selection process.
From its initial use for palette selection within the Art Rays Lite app, ProbabilitySelectors are now being used in three other areas of the code to constrain randomness and hopefully create more pleasing images. ProbabilitySelectors might be useful in your code if you can formulate the problem to be solved like I did above.
Other possible uses might be in picking directories of digital photographs for display in a digital picture frame. You might want to select pictures from a family directory 60% of the time. Pictures from your pets directory 30% of the time. Pictures from your sports car directory 10% of the time.
ProbabilitySelectors might also come in handy for software testing. You might use a ProbabilitySelector for picking suites of tests to run and then use another ProbabilitySelector to pick which tests within a suite to run. Executing test suites and test cases in an unpredictable order might point out bugs in the code you might not have seen otherwise.