The accepted answer is not doing any sanity check, and is sensitive to rounding errors. You should use randsample instead.
To sample n points from X, with replacement, and probabilities P:
randsample( X, n, true, P )
This can also be used with a custom RandStream (see documentation). Be aware that this function does NOT check for negative values in P; check manually if needed.