The best way is to use the HSV color space and distribute the colors evenly across hue (H). Use mid to full saturation (S) for distinct colors (lower saturation will be gray). You can probably also use two distinct values (V). Ref: http://en.wikipedia.org/wiki/HSL_and_HSV (includes conversion algorithms back and forth to RGB) In short, HSV colors work much more closely to how we perceive color, so it's much easier to make proper adjustments to suit your visual needs (in this case making colors distinct).
So say you start by picking 0.75 for the Saturation. Low saturation washes out to gray. High saturation makes deep or vibrant colors. Saturation should be maintained across the chosen colors or else some colors will look distinctly different and higher saturation values will appear to mean something different.
Then you decide how many Values you can have (1, 2, or maybe even 3) and still provide good clarity. Low values are dark approaching black, while high values are bright and vibrant. So maybe a Value of 0.5 and a Value of 0.8. If you go much below 0.4, you'll start to get colors so dark that they lose distinctiveness. If you use more than 2 or 4 Values the distinctiveness of colors that only differ in Value will be lost.
Then you assign as many Hues as necessary. Hue controls what we generally refer to as "color". Hues start at 0° (red) and advance through a circle back to the beginning with green at 120°, and blue at 240°. If you are using two separate Values, you will need half as many Hues as you have colors. So if you have 10 colors, half of them will be the low Value (0.4), and half of them will be the high Value (0.8). Then assign 5 equidistant Hues to go with each Value. Since Hue is a degree from 0 to 360, this means you'll use hues 0 and every 72 degrees from that (360/5 = 72). So hues 0, 72, 144, 216, and 288.
Then just convert to RGB and you're all set.
Example HSV set based on 10 needed colors and two selected Values
- 0°, 0.75, 0.4 (dark red)
- 0°, 0.75, 0.8 (bright red)
- 72°, 0.75, 0.4 (dark lime-yellow)
- 72°, 0.75, 0.8 (bright lime-yellow)
- 144°, 0.75, 0.4 (dark green w/ blue tint)
- 144°, 0.75, 0.8 (bright green w/ blue tint)
- 216°, 0.75, 0.4 (dark aqua)
- 216°, 0.75, 0.8 (bright aqua)
- 288°, 0.75, 0.4 (dark violet)
- 288°, 0.75, 0.8 (bright violet)
Once you've got the basic algorithm going with an HSVtoRGB function, you should be able to tweak it pretty easily to suit your needs.