A Monte-Carlo method for calculation of the distribution of ionization losses