Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

JVM Languages

Probability Selector

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.

Table 1: Ray tracer palettes.

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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.