This is something I've pseudo-solved many times and never quite found a solution that's stuck with me. The problem is to come up with a way to generate N colors that are as distinguishable as possible where N is a parameter.
I've read somewhere the human eye can't distinguish between less than 4 values apart. so This is something to keep in mind. The following algorithm does not compensate for this.
But this approach would significantly slow down your algorithm.
Another way would be to scrap the randomness and systematically go through every 4 values and add a color to an array in the above example.
Following the algorithm above, what if you took the wheel and divided it by the N colors you wanted. If you wanted two colors, do one at 0,0,0 and the next at 256,256,256? 3 colors is 0,0,0 next at 128,128,128, next at 256,256,256 and so on?
Naturally you'll hit some limit that it is indistinguishable, but you'll be asking for a LOT of colors at that point.
But (0,0,0) is black, (255,255,255) is white and all other colors you suggest in the form (X, X, X) are shades of gray!
The normal algorithm takes a color-wheel you sample it at equal angles. If you need N colors, you sample it at 360/N and you get a few good colors. This is really only a good solution to up ~12 or so. And there are TONS of colors that get ignored once you go passed that, while getting more and more shades of the same.
The problem is that the color wheel is only one path through the color space and so points equidistant on the color-wheel are still confined to a narrow part of the fuller color space.
Isn't it also a factor which order you set up the colors?
Like if you use Dillie-Os idea you need to mix the colors as much as possible. 0 64 128 256 is from one to the next. but 0 256 64 128 in a wheel would be more "apart"
First, is RGB the best color space?
Once you've generated N maximally separated vectors how do you sort them such that the first M are also relatively distinct.
It would be best to find colors maximally distant in a "perceptually uniform" colorspace, e.g. CIELAB (using Euclidean distance between L*, a*, b* coordinates as your distance metric) and then converting to the colorspace of your choice. Perceptually uniformity comes from tweaking the colorspace to approximate the non-linearities in the human visual system.
ColorBrewer - Sets of colours designed to be maximally distinguishable for use on maps.
Escaping RGBland: Selecting Colors for Statistical Graphics - A technical report describing a set of algorithms for generating good (i.e. maximally distinguishable) colour sets in the hcl colour space.
Here is some code to allocate RGB colors evenly around a HSL color wheel of specified luminosity.
Last I checked JFreeChart has this precise algorithm and as it is open source you can check out what it does. I do know that the colors I get do not seem to be randomly spaced along some circle or sphere, but rather chosen more specifically.