add generic function: nsamples

This is useful for generic algorithms that work on arbitrary types of
distributions (e.g. fitting a finite mixture model).