Given a stream of token probabilities, write an efficient algorithm to implement top-p nucleus sampling.
Implement sample_top_p(probabilities, p, random_value). The probabilities are aligned with token indices and sum to 1.0. Return the selected token index after retaining the smallest probability set whose cumulative probability is at least p, renormalizing that set, and sampling with random_value in [0, 1).
Use descending probability order, breaking ties by the original token index.
def sample_top_p(probabilities, p, random_value):