heavily revised according to global rand_parts function -KL