It's basically a weighted random number, with the weight being determined by the colors of the neighbors.