A stochastic simulation method for the solar cycle modulation of cosmic rays