  We present a new model for the distribution of free electrons in the Galaxy,
the Magellanic Clouds and the intergalactic medium (IGM) that can be used to
estimate distances to real or simulated pulsars and fast radio bursts (FRBs)
based on their dispersion measure (DM). The Galactic model has an extended
thick disk representing the so-called warm interstellar medium, a thin disk
representing the Galactic molecular ring, spiral arms based on a recent fit to
Galactic HII regions, a Galactic Center disk and seven local features including
the Gum Nebula, Galactic Loop I and the Local Bubble. An offset of the Sun from
the Galactic plane and a warp of the outer Galactic disk are included in the
model. Parameters of the Galactic model are determined by fitting to 189
pulsars with independently determined distances and DMs. Simple models are used
for the Magellanic Clouds and the IGM. Galactic model distances are within the
uncertainty range for 86 of the 189 independently determined distances and
within 20\% of the nearest limit for a further 38 pulsars. We estimate that
95\% of predicted Galactic pulsar distances will have a relative error of less
than a factor of 0.9. The predictions of YMW16 are compared to those of the
TC93 and NE2001 models showing that YMW16 performs significantly better on all
measures. Timescales for pulse broadening due to interstellar scattering are
estimated for (real or simulated) Galactic and Magellanic Cloud pulsars and
FRBs.
