We developed a computational technique based on Monte Carlo method to simulate the deformation of the spectra recorded by solar hard X-ray spectrometers due to pulse pile-up at high counting rates. Pile-up is simulated assuming a poissonian temporal distribution of the pulse arrivals, and also a square wave approximation to the pulse amplitude. Our code, that reproduces Datlowe's (1975) numerical algorithm when applied to thermal incident spectra, was tested also with typical power-law solar X-ray spectra, for increasing pulse input rate to the analog electronics of the pulse-height analyzer.