The evolution of mechanisms to produce phenotypic heterogeneity in microorganisms