def sample_upper_hull(upper_hull, random_stream):
cdf = cumsum([node.pr for node in upper_hull])<EOL>U = random_stream.rand()<EOL>node = next(<EOL>(node for node, cdf_value in zip(upper_hull, cdf) if U < cdf_value),<EOL>upper_hull[-<NUM_LIT:1>]  <EOL>)<EOL>U = random_stream.rand()<EOL>m, left, right = node.m, node.left, node.right<EOL>M = max(m * right, m * left)<EOL>x = (log(U * (exp(m * right - M) - exp(m * left - M)) + exp(m * left - M)) + M) / m<EOL>assert(x >= left and x <= right)<EOL>if isinf(x) or isnan(x):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return x<EOL>
Return a single value randomly sampled from
the given `upper_hull`.

Parameters
----------
upper_hull : List[pyars.hull.HullNode]
    Upper hull to evaluate.

random_stream : numpy.random.RandomState
    (Seeded) stream of random values to use during sampling.

Returns
----------
sample : float
    Single value randomly sampled from `upper_hull`.