We describe a numerical algorithm that simulates the propagation of light in inhomogeneous universes. This algorithm computes the trajectories of light rays between the observer, located at redshift z = 0, and distant sources located at high redshift using the multiple lens plane method. The deformation and deflection of light beams as they interact with each lens plane are computed using the filled-beam approximation. We use a particle-particle/particle-mesh (P3M) N-body numerical code to simulate the formation of large-scale structure in the universe. We extend the length resolution of the simulations to submegaparsec scales by using a Monte Carlo method for locating galaxies inside the computational volume according to the underlying distribution of background matter. The observed galaxy two-point correlation function is reproduced. This algorithm constitutes a major improvement over previous methods, which either neglected the presence of large-scale structure, neglected the presence of galaxies, neglected the contribution of distant matter (matter located far from the beam), or used the Zeldovich approximation for simulating the formation of large-scale structure. In addition, we take into account the observed morphology-density relation when assigning morphological types to galaxies, something that was ignored in all previous studies. To test this algorithm, we perform 1981 simulations for three different cosmological models: an Einstein-de Sitter model with density parameter Ω0 = 1, an open model with Ω0 = 0.2, and a flat, low-density model with Ω0 = 0.2 and a cosmological constant of λ0 = 0.8. In all models, the initial density fluctuations correspond to a cold dark matter power spectrum normalized to COBE. In each simulation, we compute the shear and magnification resulting from the presence of inhomogeneities. Our results are the following: (1) The magnification is totally dominated by the convergence, with the shear contributing less than one part in 104. (2) Most of the cumulative shear and magnification is contributed by matter located at intermediate redshifts, z = 1-2. (3) The actual value of the redshift at which the largest contribution to shear and magnification occurs depends on the cosmological model. In particular, the lens planes contributing the most are located at larger redshift for models with smaller Ω0. (4) The number of galaxies directly hit by the beam increases with redshift, while the contribution of lens planes to the shear and magnification decrease with increasing lens plane redshift for z > 2, which indicates that the bulk of the shear and magnification does not originate from direct hits, but rather from the tidal influence of nearby and more distant galaxies and background matter. (5) The average contributions of background matter and nearby galaxies to the shear is comparable for models with small Ω0. For the Einstein-de Sitter model, the contribution of the background matter exceeds the contribution of nearby galaxies by nearly 1 order of magnitude.