An algorithm is described which simulates a data set obtained from a protein crystal using the rotation method. The diffraction pattern of an ideal crystal is specified by the orientation of the crystal's cell axes with respect to a specified laboratory coordinate system, the distance between the crystal and the detector, the wavelength and the rotation range per frame. However, a realistic simulation of an experiment additionally requires at least a plausible physical model for crystal mosaicity and beam properties. To explore the physical basis of reflection shape and rocking-curve variation, the algorithm simulates the diffraction of a real crystal composed of mosaic blocks which is illuminated with a beam of given divergence and dispersion. Ray tracing for each reflection leads to reflection shapes and rocking curves that appear realistic. A program implementing the algorithm may be used to reproducibly generate data sets that model different physical aspects (imperfections) of the crystal and the experiment. Certain types of systematic errors of the experimental apparatus may also be simulated. Further applications include teaching and characterization of the properties of data-reduction algorithms.