The relation between magnetic field and gas density in interstellar clouds